0

Okay so I been learning with some intro vids and I'm just learning but I couldn't find the answer to this problem when I searched. Here is the code:

import random
highest = 10
answer = random.randrange(highest)
guess = raw_input("Guess a number from 0 to %d: " %highest)
while (int(guess) != answer):
    if (int(guess) < answer):
        print "Answer is higher"
    elif (int(guess) > 10):
        print "You know that number is higher than 10 right?"
    elif (int(guess) < 0):
        print "You shouldn't be guessing numbers lower than zero dummy"
    else:
        print "Answer is lower"
    guess = raw_input("Guess a number from 0 to %d: "%highest)
raw_input("You're a winner Face!!!")

Now it works fine when I put in say 22 but when I input -3 it just says "integer is higher" where it should output "You shouldn't be guessing numbers lower than zero dummy". I just put in the number like -4 but it gives me an error if I do it like this (-4). Am I missing something? Forgive me if this is really easy I'm kinda just starting to learn :)

1
  • int(guess) < answer catches negative numbers too. Commented Dec 7, 2015 at 4:37

3 Answers 3

1

The results you are getting are correct, nothing is wrong with them, this is because the sequence of if-elif-else block you decided to put, so when you have a guess number, it will check it through the if-elif-else block one by one whichever comes first and matches it will excute its code block.

You can re-arrange the order to meet your expectations, this way:

if (int(guess) < 0):  #check first if it's negative
    print "You shouldn't be guessing numbers lower than zero dummy"
elif (int(guess) < answer):
    print "Answer is higher"
elif (int(guess) > 10):
    print "You know that number is higher than 10 right?"
else:
    print "Answer is lower"
Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense and when I switched it around it actually worked how I intended it to work. I see now how the order is important so we don't meet the requirements of 2 cases but spit out the first when its really the second. Thanks!
0

if-statement in Python is execute in order if the confition is True.

guess = -4

if (int(guess) < answer):
    print "Answer is higher"

# ...

this is True, so it printed "Answer is higher'

1 Comment

Ahh I see. That makes sense. Thanks!
0

First check for the negative number and then check if the input is greater than the random number.

Lets say the integer answer is 8, and the number you input is -1. Now as per your first condition, answer>input which comes out to be true.

while (int(guess) != answer):
    if (int(guess) < 0):
        print "You shouldn't be guessing numbers lower than zero dummy"
    elif (int(guess) > 10):
        print "You know that number is higher than 10 right?"
    elif (int(guess) < answer):
        print "Answer is higher"
    else:
        print "Answer is lower"

1 Comment

This worked when I tried it from the person who answered above. Thanks for the quick response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.