python - 如何在python的while循环内停止for循环
问题描述
我正在制作一个简单的运动员比赛时间数据输入表单,我需要它询问每次用户是否要继续,如果是,那么它会再次执行,如果不是,则退出 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 循环不确定是否需要 while
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")
推荐阅读
- r - 如何从数据框中简单地生成编号列表?
- docker - 制作一个与旧容器具有相同配置的新容器
- ruby - 在 ruby 程序中使用 capybara 和 poltergeist 访问 https 站点时出现“JQMIGRATE”错误
- concurrency - 如何以原子方式检查映射中是否存在键,如果不存在则添加它
- php - foreach 循环输出中的连续数组键
- javascript - 如何从 react 和 redux 将图像传递到后端
- r - 从大型数据集重新采样的 Diff-in-diff 估计
- javascript - 如何在 Javascript 中使用“ArrowUp”增加表情符号的大小
- sql - SQL - 转换为日期
- node.js - 语法错误:意外的标记 { 在编译的打字稿上