Creating a calculator

Elizabeth Weiss cake240 at gmail.com
Thu Jun 30 23:08:42 EDT 2016


while True:
	print("Options:")
	print("Enter 'add' to add two numbers")
	print("Enter 'subtract' to subtract two numbers")
	print("Enter 'multiply' to multiply two numbers")
	print("Enter 'divide' to divide two numbers")
	print("Enter 'quit' to end the program")
	user_input=input(":")
	if user_input=="quit":
		break
	elif user_input=="add":
		num1=float(input("Enter a number"))
		num2=float(input("Enter another number"))
		result=str(num1+num2)
		print("The answer is"+ result)
	elif user_input=="subtract":
		num1=float(input("Enter a number"))
		num2=float(input("Enter another number"))
		result=str(num1-num2)
		print("The answer is"+result)

Two questions:
1. Why do I need to put ' ' around the words add, subtract, multiply, quit, etc. when it is already in quotes in print()? When the calculator asks me which option I would like to choose I do not write 'add'- I only write add. 

2. The program I am using to help me learn python mentions that the output line could be put outside the if statements to omit repetition of code. What does this mean and how would I write the code differently according to this?

Thank you!



More information about the Python-list mailing list