虽然循环迭代太多次

时间:2017-03-14 00:44:19

标签: python while-loop iteration

我有四个输入语句,可以创建一个简单的计算。我希望用户能够在第一个提示中键入0以便立即停止程序,但程序会在停止之前让用户完成整个循环。如何使程序在键入0之类的命令后直接停止迭代?

def main():
      input_1 = 1
      input_2 = 1
      input_3 = 1
      input_4 = 1
      while input_1 !=0:
           input_1 = int(input('Please enter a value or type 0 to end: '))
           input_2 = int(input('Please enter a second value: '))
           input_3 = int(input('Please enter a third value: '))
           input_4 = int(input('Please enter a fourth value: '))
           print('The total amount is: ', end='')      
           print(all_inputs(input_1,input_1,input_1,input_1), end='')


def all_inputs(first,second,third,fourth):
     sum = first + second + third + fourth
     return(sum)
main()

3 个答案:

答案 0 :(得分:0)

您可以退出input_1 == 0或将input_1之后的所有内容包装在if中仅在input_1!= 0时执行。

def main():
  input_1 = 1
  input_2 = 1
  input_3 = 1
  input_4 = 1
  while input_1 !=0:
       input_1 = int(input('Please enter a value or type 0 to end: '))
       if input_1 != 0 :
           input_2 = int(input('Please enter a second value: '))
           input_3 = int(input('Please enter a third value: '))
           input_4 = int(input('Please enter a fourth value: '))
           print('The total amount is: ', end='')      
           print(all_inputs(input_1,input_1,input_1,input_1), end='')


def all_inputs(first,second,third,fourth):
 sum = first + second + third + fourth
 return(sum)

答案 1 :(得分:0)

虽然@FredMan是正确的,但我觉得你好像可以整理你的循环。我不是一个Python人,但这就是我想出的:

def main():
      keep_going = '';
      while keep_going != 'n':
           input_1 = int(input('Please enter a value: '))
           input_2 = int(input('Please enter a second value: '))
           input_3 = int(input('Please enter a third value: '))
           input_4 = int(input('Please enter a fourth value: '))
           print('The total amount is: ', end='')
           # In your example, you sum input_1 four times
           # \n at the end adds a newline      
           print(all_inputs(input_1,input_2,input_3,input_4), end="\n")
           keep_going = input('Keep going? y/n: ')


def all_inputs(first,second,third,fourth):
     sum = first + second + third + fourth
     return(sum)
main()

还有很多其他方法可以清理它,但我觉得这是一个简单的解决方案,可以保持你想要的风格:)

编辑:这是另一种解决方法

def main():
  keep_going = '';
  while keep_going != 'n':
    answers = []
    answers.insert(0, input('Please enter a value: '))
    answers.insert(0, input('Please enter a second value: '))
    answers.insert(0, input('Please enter a third value: '))
    answers.insert(0, input('Please enter a fourth value: '))

    total = sum([*map(int, answers)])
    print("The total amount is: {}".format(total))      
    keep_going = input('Keep going? y/n: ')

main()

答案 2 :(得分:0)

我不是一个Python用户,但我知道你的问题在哪里。

input_1,2,3和4的预设值在循环内完成。

为什么不将loop_1置于循环外部而不是循环内部。这样一旦你运行该函数,它将通过while look检查input_1!= 0,如果是,则循环将立即终止。

实际上,为什么你需要一个循环,因为你已经用四个独立的变量收集了4个参数。你可以这样做

def main:
    input_1 = int(input('Please enter first value: '))
    input_2 = int(input('Please enter a second value: '))
    input_3 = int(input('Please enter a third value: '))
    input_4 = int(input('Please enter a fourth value: '))
    print('The total amount is: ', end='')      
    print(all_inputs(input_1,input_2,input_3,input_4), end='')

def all_inputs(first,second,third,fourth):
    sum = first + second + third + fourth
    return(sum)

main()