In: Computer Science
In Python, write a calculator that will give the user the following menu options:
1) Add
2) Subtract
3) Multiply
4) Divide
5) Exit
Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place.
Example 1:
Let's calculate!
1) Add
2) Subtract
3) Multiply
4) Divide
5) Exit
Please select one of options above: 4
Enter the first number: 2.81
Enter the second number: 1.111
Answer: 2.5
1) Add
2) Subtract
3) Multiply
4) Divide
5) Exit
Please select one of options above: 5
Have a good day!
Program:
print("\nLet's calculate!")
while(True):
print("\n1) Add")
print("2) Subtract")
print("3) Multiply")
print("4) Divide")
print("5) Exit")
option=int(input("\nPlease select one of options
above: "))
if(option==1):
no1=float(input("Enter the first
number: "))
no2=float(input("Enter the second
number: "))
print("Answer:
%.1f"%(no1+no2))
elif(option==2):
no1=float(input("Enter the first
number: "))
no2=float(input("Enter the second
number: "))
print("Answer:
%.1f"%(no1-no2))
elif(option==3):
no1=float(input("Enter the first
number: "))
no2=float(input("Enter the second
number: "))
print("Answer:
%.1f"%(no1*no2))
elif(option==4):
no1=float(input("Enter the first
number: "))
no2=float(input("Enter the second
number: "))
print("Answer:
%.1f"%(no1/no2))
elif(option==5):
print("Have a good day!\n")
break
else:
print("Invalid option!!\n")
Program Screenshot:
Output: