【问题标题】:How do I repeat my initial input code?如何重复我的初始输入代码?
【发布时间】:2013-01-11 09:06:39
【问题描述】:

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

【问题讨论】:

  • 您运行的是 Python 2 还是 Python 3?你有两个标签。

标签: python python-2.7


【解决方案1】:

您可以将该代码拆分为一个函数:

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

【讨论】:

  • 我假设它是 Python 3,因为 print() 是一个函数,并且没有来自 __future__ 的导入
  • @Volatility 是针对我的,还是应该将此评论放在其他用户询问它是 2 还是 3 的顶部
  • 好吧,我猜是针对你们俩,但您提到了raw_input(),所以它主要针对您。我同意将两个标签都放在那里有点令人困惑。
  • @Volatility 我放回了 int()
  • 谢谢!这正是我想要寻找的!
【解决方案2】:

使用while 循环:

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

while True:
    #your code

    elif x == 5:       # no need of () around conditions
    print ("goodbye")
    break             # exit the while loop 

【讨论】:

  • 谢谢我想通了!我在操作代码中尝试了while循环,而不是在整个程序的开头!
【解决方案3】:

您需要阅读基本的流量控制。这是相关的python tutorial section

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

for a in range(10):
    print a

b=0
while b<0:
    print b
    b= b+1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 2020-06-30
    • 2021-08-16
    • 2011-11-14
    • 1970-01-01
    • 2020-05-21
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多