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

Gri*_*per 1 python loops range python-3.x

我遇到了一个问题,我的代码跳过for所有输入提示的循环,并直接打印循环下方的列表。

出了什么问题,我该如何解决?

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)
Run Code Online (Sandbox Code Playgroud)

mkr*_*er1 6

range(0, )相当于range(0)(另请参阅:我应该在函数调用的最后一个参数后添加尾随逗号吗?)。

但是range(0)是一个空范围。对其进行迭代会导致循环次数为零,即for跳过整个循环。

如果您希望循环一直持续到break内部遇到语句,请使用while True:代替for i in range(0, ):

您似乎没有使用该i变量,但如果您确实想计算此类循环中的迭代次数,则可以使用itertools.count(请参阅:保持无限计数的简单方法在 while 循环中计算迭代次数):

>>> import itertools
>>> for i in itertools.count():
...     print(i)
...     if i > 3:
...         break
... 
0
1
2
3
4
Run Code Online (Sandbox Code Playgroud)