如何在python中停止for循环,在while循环中

How to Stop a for loop, inside of a while Loop in python

我正在制作一个简单的运动员比赛时间数据输入表格,我需要它询问每一次传球是否用户想要继续,如果是,那么它再次进行,如果不是那么它退出while循环。 如果用户没有输入至少4-8个数据,那么它会产生错误而不是打印出时间。 我相信错误是由于它第一次通过while循环后,它在for循环中执行8之前不再执行另一次传递。 我该如何解决这个问题。 请解释您的代码,并将其与我给出的上下文联系起来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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循环
不确定是否需要

1
2
3
4
5
6
7
8
9
10
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

//按照你的代码和你提出的要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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循环和break指令:

1
2
3
4
5
6
7
8
9
10
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

注意可以写入范围(0,8):范围(8)

并且输入返回一个字符串,因此str(input(...))是无用的。

此外:

1
if 3 < i > 9

手段:

1
if i > 3 and i > 9:

我想你的意思是:

1
if 3 < i < 9:

最后:如果用户输入的不是数字,float(input(...))可能会引发ValueError异常。 你应该添加一个try:except:construct。


for循环无论如何都会进行8次迭代,因此您将始终输入8个通道。
您可以完全删除for循环,并用一个简单的计数器替换它。
当用户选择添加另一个通道时,增加计数器,当它达到8时 - 结束循环。
像(在伪代码中)的东西:

1
2
3
4
5
6
7
8
9
10
11
counter =0
while carry_on
  <read user input>
  if counter < 8
   
  if decision =="yes"
    counter++
    carry_on = true
  else
    carry_on = false
<handle input here>