python while循环stu

2024-05-19 18:19:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这是基本的,但我不能让它发挥作用。 它似乎不理解大于60,并且不断地循环。在

Python 2.7.3

rackNo = 0
while (rackNo > 60) or (rackNo < 1) :
    rackNo = raw_input("Enter the Rack number you are using: ") 
    if (rackNo > 60) or (rackNo < 1) :
        print "Rack number must be between 1 and 60"

Tags: ortheyounumberinputrawifare
1条回答
网友
1楼 · 发布于 2024-05-19 18:19:30

将字符串(from raw_input)与整数进行比较。在

最终,你想要的是:

rackNo = int(raw_input("Enter the Rack number you are using: "))

在python2.x中,内置类型之间的比较(><)依赖于实现。在python3.x中,这些比较是明确禁止的。在

(python2.x documentation)

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily. You can control comparison behavior of objects of non-built-in types by defining a __cmp__ method or rich comparison methods like __gt__, described in section Special method names.

(python3.x documentation)

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, the == and != operators always consider objects of different types to be unequal, while the <, >, >= and <= operators raise a TypeError when comparing objects of different types that do not implement these operators for the given pair of types. You can control comparison behavior of objects of non-built-in types by defining rich comparison methods like __gt__(), described in section Basic customization.

相关问题 更多 >