+ 1

Please HELP!

Can someone please help me shorten/simplify this programme? Thanks in advance!! print ('Hello World') name = input("\nWhat\'s your name? : ") print ("\nHey there, ", name, ". \n Welcome to my first programme : A Simple Calculator") def add(x,y): ans = x + y return ans def minus(x,y): ans = x - y return ans def multiply(x,y): ans = x * y return ans def divide(x,y): ans = x / y return ans def power(x,y): ans = x ** y return ans def remainder(x,y): ans = x % y return ans x = int(input("\nEnter number one: ")) y = int(input("\nEnter number two: ")) function = str(input("\n\nType: \n + for addition \n - for subtraction \n x for multiplication \n / for divison \n ** for indices \n % for remainder \n")) print ("\n") if function == "+": print (x, "+", y, "=", (add(x,y))) elif function == "-": print (x, "-", y, "=", (minus(x,y))) elif function == "x": print (x, "x", y, "=", (multiply(x,y))) elif function == "/": print (x, "/", y, "=", (divide(x,y))) elif function == "**": print (x, "to the power of", y, "=", (power(x,y))) elif function == "%": print ("The remainder of ", x, "divided by ", y, "equals ", (remainder(x,y))) else : print ("Uhmm, did you mis-spell something??")

26th Aug 2017, 4:57 PM
Simon Luo
Simon Luo - avatar
2 ответов
+ 1
You can shorten all functions like this: def add(x, y): return x+y
26th Aug 2017, 11:05 PM
Anselm
0
Thanks Anselm! I appreciate your answer, I have just started coding!
27th Aug 2017, 2:51 PM
Simon Luo
Simon Luo - avatar