循环被跳过

时间:2014-05-15 20:29:52

标签: python-2.7 while-loop

我似乎无法在我的代码中运行我的while循环代码。我打赌这很明显,但我似乎找不到答案。这个程序可以让你选择你想要随机选择的数字和它们之间的数字。似乎while循环并不想工作。它会跳过while循环并进入睡眠状态(10)。谢谢你的帮助!

import random
import time
from time import sleep
x = raw_input("Enter first number you want to be the minimum: ")
y = raw_input("Enter second number you want to be the maximum: ")
a = raw_input("Enter ammount of random numbers you want: ")
p = 1
while p >= a:
    print "Your number is " + str(int(random.randint(x - 1,y + 1)))
    p = p + 1
sleep(10)

1 个答案:

答案 0 :(得分:4)

raw_input返回一个字符串。这意味着您要将字符串与while条件的整数进行比较。快速测试显示整数总是“小于”字符串。

>>> 10000 > '1'
False
>>> 10000 < '1'
True

幸运的是,python3中的行为发生了变化,导致TypeError

相关问题