user2200692
user2200692

Reputation: 3

python while loop stuck

I know this is basic but I cannot get this to work. It does not seem to understand greater than 60 and constantly goes round in the loop.

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"

Upvotes: 0

Views: 227

Answers (1)

mgilson
mgilson

Reputation: 310227

You're comparing a string (from raw_input) to an integer.

Ultimately, you want something like:

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

In python2.x, comparison (>, <) between builtin types is implementation dependent. In python3.x, these comparisons are explicitly disallowed.

(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.

Upvotes: 8

Related Questions