我试图让我的 while
循环在程序退出之前最多循环 3 次,同时通知用户剩余的尝试。
def main():
valid = 0
while (valid == 0):
valid = checkValid ()
print ('and the program continues on with User_Input2()...')
#User_Input2()
这是 checkValid()
def checkValid():
if ((iVelocity < 20) or (iVelocity > 800) or (iTrajectory < 5) or (iTrajectory > 80)):
result = 0
else:
result = 1
return result
主程序是什么样的:
iVelocity = float(input('Please enter an initial velocity between 20 to 800 m/s: ' ))
iTrajectory = float(input('Please enter an initial trajectory angle between 5 to 80 degrees: '))
main()
我不确定在任一函数中的何处添加代码以使其循环(或者如果我需要按照 def counter():
创建一些内容),请通知用户剩余的尝试次数,以及如果所有 3 次尝试都已用完如何退出程序。
最佳答案
尝试使用带有 break
命令的 for
循环:
for i in range(3):
valid =checkValid()
if(valid==1):
break
关于python - 将 While 循环设置为最多循环 3 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35611899/