Reputation: 13
print("\nMy name is and this is my simple calculation. Enjoy!")
print("Main Menu \n
1: Addition\n
2: Subtraction\n
3: Multiplication\n
4: Division\n
5: Exit")
def add(num1, num2): return (num1 + num2)
def subtract(num1, num2): return (num1 - num2)
def multiply(num1, num2): return (num1 * num2)
def divide(num1, num2): if num2 == 0: print("Math ERROR, can not divide by 0") else: return (num1/num2)
if name == "main": while True: choice = input("\nEnter a choice: ")
if choice in ("1", "2", "3", "4"):
num1 = float(input("\nEnter number 1: "))
num2 = float(input("Enter number 2: "))
else choice == 5:
print("Thank you for using calc app")
break
if choice == "1":
print("The answer is:",add(num1, num2))
elif choice == "2":
print("The answer is:",subtract(num1, num2))
elif choice == "3":
print("The answer is:",multiply(num1, num2))
elif choice == "4":
print("The answer is:",divide(num1, num2))
else:
print("Wrong choice... please try again..")
Upvotes: 0
Views: 86
Reputation: 412
The break
keyword only serves to "break" out of a while
or for
loop. In order to exit the program you should use the exit()
function.
Upvotes: 1
Reputation: 76
It looks like you are trying to make a simple calculator program...
If I am understanding your flow, the user enters a number 1-5 to preform a specific calculation, then if they chose 1-4, they are prompted to enter two more numbers.
It looks like the problem is that you are trying to do the calculations in the wrong branch of your code. If you use a while loop properly, you can avoid having to use a break statement to exit the code.
I would structure it like this:
choice = ""
while choice != "5":
choice = input("\nEnter a choice: ")
if choice in ("1","2","3","4"):
// prompt user for two more numbers and store in num1 and num2
if choice == "1":
print("The answer is: ", addNum(num1,num2))
if choice == "2":
// do stuff
if choice == "3":
// do stuff
if choice == "4":
// do stuff
elif choice == "5":
print("Thanks for using calculator! Exiting now...")
else:
print("Wrong choice, please try again")
Upvotes: 2