我正在使用python中的计算器作为我的课程作业之一(基本操作,加法,减法,乘法和除法)我有一个打印菜单,用户可以从中选择他们想要使用的操作。然后输入他们的整数和答案是什么的打印声明。我的问题是我需要重复我对用户想要执行的操作的初始输入。编程新手,所以非常感谢任何帮助。
这是我的代码:
print ("1 = addition")
print ("2 = subtraction")
print ("3 = multiplication")
print ("4 = division")
print ("5 = Exit program\n")
x = int (input ("What operation would you like to perform?: ")) #prompts user for operation
if (x == 1): #if operation chose is addition then this line will exacute
int1 = input ("Enter first integer: ")
int1 = int (int1)
int2 = input ("Enter second integer: ")
int2 = int (int2)
sum = int1 + int2
print ("Sum is:", sum)
elif (x == 2): #if operation chose is subtraction then this line will exacute
int1 = input ("Enter first integer: ")
int1 = int (int1)
int2 = input ("Enter second integer: ")
int2 = int (int2)
dif = int1 - int2
print ("Difference is:", dif)
elif (x == 3): #if operation chose is multiplication then this line will exacute
int1 = input ("Enter first integer: ")
int1 = int (int1)
int2 = input ("Enter second integer: ")
int2 = int (int2)
mult = int1 * int2
print ("Multiplication is:", mult)
elif (x == 4): #if operation chose is division then this line will exacute
int1 = input ("Enter first integer: ")
int1 = int (int1)
int2 = input ("Enter second integer: ")
int2 = int (int2)
div = int1 / int2
print ("Division is: %.2f" % div)
elif (x == 5):
print ("goodbye")
quit()
答案 0 :(得分:1)
使用while
循环:
现在这将一次又一次地循环,直到x
不等于5
while True:
#your code
elif x == 5: # no need of () around conditions
print ("goodbye")
break # exit the while loop
答案 1 :(得分:0)
您需要了解基本的流量控制。这是相关的python tutorial section。
这可以使用for
或while
循环来实现。以下是两者的例子:
for a in range(10):
print a
b=0
while b<0:
print b
b= b+1
答案 2 :(得分:0)
您可以将该代码拆分为一个函数:
print "1 = addition"
print "2 = subtraction"
print "3 = multiplication"
print "4 = division"
print "5 = Exit program\n"
def maths(choice):
if choice == 1: #if operation chose is addition then this line will exacute
int1 = input("Enter first integer:")
int2 = input ("Enter second integer: ")
sum = int1 + int2
print "Sum is:", sum
elif choice == 2: #if operation chose is subtraction then this line will exacute
int1 = input("Enter first integer: ")
int2 = input("Enter second integer: ")
dif = int1 - int2
print "Difference is:", dif
elif choice == 3: #if operation chose is multiplication then this line will exacute
int1 = input("Enter first integer: ")
int2 = input("Enter second integer: ")
mult = int1 * int2
print "Multiplication is:", mult
elif choice == 4: #if operation chose is division then this line will exacute
int1 = input("Enter first integer: ")
int2 = input ("Enter second integer: ")
div = int1 / int2
print "Division is: %.2f" % div
elif choice == 5:
print "goodbye"
quit()
while True: # "While True, repeat everything below":
maths(input("What operation would you like to perform?: "))
编辑:看来你已经声明它是python 2.7,所以我为你清理了代码。
您不需要int(int1)和int(int2),因为input()不会使输入成为字符串。即:
>>> number = input('Enter a number! ') # Let's say I put 5
>>> print number
5 # Notice it's not a string (no ' ')
而raw_input()会这样做:
>>> number = raw_input('Enter a number! ')
>>> print number
'5' # 5 is a string, not an integer. And so int() would be required