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

时间:2018-05-04 06:40:24

标签: python python-3.x for-loop while-loop boolean

我正在制作一个简单的运动员比赛时间数据输入表格,我需要它询问每个传球是否用户想要继续,如果是,那么它再次进行,如果没有则它退出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")

3 个答案:

答案 0 :(得分:2)

首先,学习基础知识

尝试中断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")

答案 1 :(得分:1)

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>

答案 2 :(得分:1)

你只能使用一个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

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

输入返回一个字符串,因此str(input(...))没用。

此外:

if 3 < i > 9

表示:

if i > 3 and i > 9:

我认为你的意思是:

if 3 < i < 9:

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