python:while循环不检查条件

nom*_*ons -3 python

这是我的代码:

l=raw_input()
x=l.split(' ')
n=x[0]
w=x[1]
l=raw_input()
i=0
print n
while(i<n):
    print "1 %d" %i
    i=i+1
Run Code Online (Sandbox Code Playgroud)

和输入一样

6 6
1 2 3 4 5 3
Run Code Online (Sandbox Code Playgroud)

它给出了运行时错误.

虽然n被分配了6并i从0开始,但为什么在i = 6时没有结束?价值i增加超过n无限.

Mar*_*ers 5

n是一个字符串; raw_input()返回字符串,然后在空格上分割.i是一个整数.比较时,Python不强迫字符串整数和字符串之前总是排序号码,所以i<n永远正确:

>>> 6 < '6'
True
Run Code Online (Sandbox Code Playgroud)

转换n为整数:

n = int(x[0])
Run Code Online (Sandbox Code Playgroud)