简体   繁体   English

为什么我的代码会跳过整个“for i in range”循环?

[英]Why is my code skipping the entire “for i in range” loop?

I've come across an issue where my code is skipping the for loop with all the input prompts and is going straight to the printing of the lists below the loop.我遇到了一个问题,我的代码正在跳过所有输入提示的for循环,并直接打印循环下方的列表。

What is wrong and how can I fix this?出了什么问题,我该如何解决?

import statistics

Age = list()
SaturnRT = list()
MarsRT = list()
Mars = list()
Saturn = list()
Houses = ['saturn', 'Saturn', 'Mars', 'mars']
CheckList = ['Saturn', 'saturn']
ReactionTime = list()
inputVar = None


for i in range(0, ):
    print("enter age: ")
    while inputVar != type(int):
        try:
            inputVar = int(input())
            while inputVar>16 or inputVar<12:
                print("error! invalid entry")
                inputVar = int(input("enter the age: "))
            break
        except:
            print("enter an integer! ")
    Age.append(inputVar)

    print("enter reaction time (ms): ")
    while inputVar != type(float):
        try:
            inputVar = float(input())
            while inputVar < 0 or inputVar > 500:
                print("enter a valid time! ")
                inputVar = float(input())
            House = str(input("enter the house: "))
            while House not in Houses:
                print("error! invalid entry")
                House = str(input("enter the house: "))
            if House in CheckList:
                SaturnRT.append(inputVar)
                Saturn.append(House)
            else:
                MarsRT.append(inputVar)
                Mars.append(House)
            break
        except:
            print("enter an valid time! ")
    ReactionTime.append(inputVar)

print(SaturnRT)
print(MarsRT)
print("saturn reaction times avg: ", statistics.mean(SaturnRT))
print("saturn fastest time: ", min(SaturnRT))
print("saturn slowest time: ", max(SaturnRT))
print("mars reaction times avg: ", statistics.mean(MarsRT))
print("mars fastest time: ", min(MarsRT))
print("mars slowest time: ", max(MarsRT))

print(ReactionTime)
print(Age)

range(0, ) is equivalent to range(0) (see also: Should I add a trailing comma after the last argument in a function call? ). range(0, )等效于range(0) (另请参阅: 我是否应该在 function 调用中的最后一个参数之后添加一个尾随逗号? )。

But range(0) is an empty range.但是range(0)是一个空范围。 Iterating over it results in a loop with zero iterations, ie, the whole for loop is skipped.对其进行迭代会导致一个零次迭代的循环,即跳过整个for循环。

If you want a loop that goes on until a break statement is encountered inside, use while True: instead of for i in range(0, ): .如果您想要一个一直持续到内部遇到break语句的循环,请使用while True:而不是for i in range(0, ):

You seem to be not using the i variable, but if you do want to count the iterations in such a loop, you can use itertools.count (see: Easy way to keep counting up infinitely or Count iterations in while loop ):您似乎没有使用i变量,但是如果您确实想在这样的循环中计算迭代次数,您可以使用itertools.count (请参阅: 保持无限计数的简单方法在 while 循环中计算迭代次数):

>>> import itertools
>>> for i in itertools.count():
...     print(i)
...     if i > 3:
...         break
... 
0
1
2
3
4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM