0

How to fix my calculator

So as an early project I thought I could create a calculator, I could but I needed the code to repeat after each calculation. It did for each 'if' statement, but the else statement breaks it: Function = (input("+, -, * or /: ")) if Function == ("+"): Answer = int(input("First number to add: ")) + int(input("Second number to add: ")) print(Answer) Function = (input("+, -, * or /: ")) if Function == ("-"): Answer = int(input("First number: ")) - int(input("Number to subtract: ")) print(Answer) Function = (input("+, -, * or /: ")) if Function == ("*"): Answer = int(input("First number: ")) * int(input("Number to multiply by: ")) print(Answer) Function = (input("+, -, * or /: ")) if Function == ("/"): Answer = int(input("First number: ")) / int(input("Number to divide by: ")) print(Answer) Function = (input("+, -, * or /: ")) else: print("Please enter a valid function") Function = (input("+, -, * or /: ")) If anyone has any suggestions please give them to me!

16th Jun 2018, 8:06 PM
BlueCode
BlueCode - avatar
5 Antworten
0
#Wrap the statement in a while loop. #Something like: Function=None while Function != "exit": Function = input("...") if Function == "+" : ...... #The loop will break, if the user type "exit". Else, the code will keep running after each calculation.
16th Jun 2018, 10:19 PM
李立威
17th Jun 2018, 12:36 AM
John Wells
John Wells - avatar
+ 1
I think it should be if Function == ("+"): and then use elif instead of if (and leave the final else as it is right now)
16th Jun 2018, 8:13 PM
Alexandru Turculet
Alexandru Turculet - avatar
0
Right, I've added the while loop, and it runs repeatedly but the calculation will only work every other time...
16th Jun 2018, 11:31 PM
BlueCode
BlueCode - avatar
0
And thank you for your responses
16th Jun 2018, 11:34 PM
BlueCode
BlueCode - avatar