[英]How to Stop a for loop, inside of a while Loop in python
我正在生成一个简单的运动员比赛时间数据输入表,我需要它询问每个通行证用户是否要继续,如果要继续,则再次进行,如果不继续,则退出while循环。 如果用户未输入至少4-8条数据,则会产生错误,而不是打印时间。 我相信该错误是由于它在第一次通过while循环之后导致的,直到在for循环中执行了8次之后,它才执行另一遍操作。 我将如何解决这个问题。 请解释您的代码,并将其与我提供的上下文相关联。
import time
datasets= []
carry_on = True
while carry_on == True:
for i in range(0, 8):
print("Inputting Data for Lane", i)
gender = str(input("Is the athlete male or female "))
athlete = str(input("What is the athletes name "))
finishTime = float(input("What was the finishing time "))
dataset = [gender, athlete, finishTime]
datasets.append(dataset)
decision = input("Would you like to add another lane ")
if decision == "yes":
carry_on = True
else:
carry_on = False
print("")
if 3 < i > 9:
print("{0:<10}{1:<10}{2:<15}".format("Gender","Athlete","Finish time"))
ds = sorted(datasets, key=lambda x:x[2], reverse=False)
for s in ds:
time.sleep(1)
print("{0:<10}{1:<10}{2:<15}".format(s[0], s[1], s[2]))
else:
print("You have not chosen enough lanes, please choose atleast 4")
首先,学习基础
尝试中断for循环,不确定是否需要
for i in range(0, 8):
print("Inputting Data for Lane", i)
gender = str(input("Is the athlete male or female "))
athlete = str(input("What is the athletes name "))
finishTime = float(input("What was the finishing time "))
dataset = [gender, athlete, finishTime]
datasets.append(dataset)
decision = input("Would you like to add another lane ")
if decision != "yes":
break
//根据您的代码和您的要求
import time
datasets= []
carry_on = True
while carry_on == True:
for i in range(0, 8):
print("Inputting Data for Lane", i)
gender = str(input("Is the athlete male or female "))
athlete = str(input("What is the athletes name "))
finishTime = float(input("What was the finishing time "))
dataset = [gender, athlete, finishTime]
datasets.append(dataset)
decision = input("Would you like to add another lane ")
if decision == "yes":
carry_on = True
else:
carry_on = False
break
print("")
if 3 < i > 9:
print("{0:<10}{1:<10}{2:<15}".format("Gender","Athlete","Finish time"))
ds = sorted(datasets, key=lambda x:x[2], reverse=False)
for s in ds:
time.sleep(1)
print("{0:<10}{1:<10}{2:<15}".format(s[0], s[1], s[2]))
else:
print("You have not chosen enough lanes, please choose atleast 4")
无论如何,for循环都会执行8次迭代,因此您将始终输入8条通道。 您可以完全删除for循环,并用一个简单的计数器替换它。 当用户选择添加另一个车道时,增加计数器,当它达到8时-结束循环。 类似于(用伪代码):
counter =0
while carry_on
<read user input>
if counter < 8
<ask user to continue>
if decision == "yes"
counter++
carry_on = true
else
carry_on = false
<handle input here>
您只能使用一个for循环和break指令:
for i in range(8):
print("Inputting Data for Lane", i)
gender = input("Is the athlete male or female ")
athlete = input("What is the athletes name ")
finishTime = float(input("What was the finishing time "))
dataset = [gender, athlete, finishTime]
datasets.append(dataset)
decision = input("Would you like to add another lane ")
if decision != "yes":
break
请注意,可以写入range(0,8):range(8)
输入返回一个字符串,因此str(input(...))是无用的。
此外:
if 3 < i > 9
手段:
if i > 3 and i > 9:
我认为您的意思是:
if 3 < i < 9:
最后:如果用户输入的不是数字,则float(input(...))可能会引发ValueError异常。 您应该添加一个try:except:construct。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.