Python 当循环迭代一次太多次时
我有四个输入语句,它们创建了一个简单的计算。我希望用户能够在第一个提示中键入0,以便立即停止程序,但程序会在停止之前引导用户完成整个循环。如何使程序在键入类似于0的命令后直接停止迭代Python 当循环迭代一次太多次时,python,while-loop,iteration,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 va
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()
您可以在输入_1==0时退出,也可以将输入_1后的所有内容包装在if中,以便仅在输入_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)
虽然@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()
我不是一个Python用户,但我知道你的问题在哪里 输入_1、2、3和4的预设值在循环内完成 为什么不把输入_1放在循环外,而放在循环内呢。这样,一旦您运行该函数,它将通过while-look检查是否输入_1!=如果为0,则循环将立即终止 事实上,既然您已经收集了4个带有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()
从循环中中断
,或者使用一个do…while
循环。我看不出有任何问题,当我运行代码时,请在第一个参数中给出0
,它应该会起作用fine@Nilesh如果输入为0,海报希望循环立即终止,而不是运行所有问题。@KieranE我在再次阅读时明白了这一点:)谢谢大家的帮助!