1

我正在使用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()
4

3 回答 3

1

使用while循环:

现在这将一次又一次地循环,直到x不等于 5

while True:
    #your code

    elif x == 5:       # no need of () around conditions
    print ("goodbye")
    break             # exit the while loop 
于 2013-01-28T03:34:48.930 回答
0

您需要阅读基本的流量控制。这是相关的python教程部分

这可以使用 aforwhile循环来实现。以下是两者的示例:

for a in range(10):
    print a

b=0
while b<0:
    print b
    b= b+1
于 2013-01-28T03:34:52.547 回答
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() 不会使输入成为字符串。IE:

>>> 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
于 2013-01-28T03:37:32.833 回答