Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

python - How do I make a variable detect if it is greater than or less than another one?

I am currently learning Python, and I decided to build a small "Guess the Number" type of game. I am using the random feature, and trying to make it so it will detect if the user's input is equal the the random number. This is my code:

irand = randrange(1, 10)
while True:
    number = input ("Pick a number 1-10: ")
    if number < irand:
        print ("    ")
        print ("That's too high, try again.")
    if number > irand:
        print ("    ")
        print ("That's too low, try again.")
    if number == irand:
        print ("    ")
        print ("You got it right! You won!")
        break

I am not exactly sure how to make it detect if number is equal to, less than or greater than irand. Any help? Thank you! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code to check "equal to, less than, or greater than" is correct, but you've got a problem earlier in the code.

In Python (3.0 and later), input returns a string. So, you're comparing number, a string, to irand, a number. Some languages might convert one way or the other automatically, but that can be confusing,*, so Python refuses to do so, and instead raises a TypeError: unorderable types: int() < str().

The solution is to parse the input into a number, like this:

number = int(number)

This will raise a ValueError if the user gives you invalid input, like the letter c instead of a number from 1 to 10. If you want to deal with that more nicely, you use a try statement.

As a side note, you probably want elif instead of if. If you've got everything right, this doesn't make any difference, but if you've made a mistake, it helps catch the mistake.

So, putting it all together:

while True:
    number = input("Pick a number 1-10: ")
    try:
        number = int(number)
    except ValueError:
        print(number, 'is not a number, try again.')
        continue
    if number < irand:
        print("    ")
        print("That's too high, try again.")
    elif number > irand:
        print("    ")
        print("That's too low, try again.")
    else:
        print("    ")
        print("You got it right! You won!")
        break

(Notice that I used continue in the except clause, so we skip over the rest of the loop and don't have to worry about number not being a number anymore. We could also move the whole loop into an else cause on the try, or add an isinstance check on each if, etc., but that gets a bit clumsy.)


* Consider comparing the string "2" to the number 10. A language that converts the string to a number will say that 2 < 10; a language that converts the number to a string will say that "10" < "2". Neither one is "right" or "wrong", but both are surprising half the time…


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...