0

So I'm very new to Python and I'm trying to my "Hello World" game to test what I've learned. in the process I'm trying to challenge myself and make it EVEN more simple than it already is and clean up my code a lot. But whilst getting my code off the ground I ran into the problem that my while loop isn't updating and looping correctly. Please take a look

import random
Ran = random.randrange(1,100)
guess = raw_input("Go ahead: ")

while (guess != Ran):
    guess = raw_input("Try again: ")

print "You got it!"

So basically it's just to see if the guess var is equal to the rand number but when it sees, "oh no you are not equal to that random number sorry you get passed to else." "Hi I'm else you are not the right number to get out our loop. So this a new input box and it is going to change the value of guess then send you back to while for checking."

But somewhere along the line it just passes right through while and ends up at else Can anyone help me figure out why this is happening? I really appreciate it :)

Python ver = 2.7.12

idjaw
  • 25,487
  • 7
  • 64
  • 83
  • 2
    Because a string is never an integer. – Andrew Li Nov 17 '16 at 03:49
  • hey, they can be if you `ast.literal_eval` them! – n1c9 Nov 17 '16 at 03:55
  • please add " int(guess)" in the while loop condition – Afsal Salim Nov 17 '16 at 04:03
  • 1
    @n1c9 Why would you do that instead of simply calling `int` on the string? – idjaw Nov 17 '16 at 04:08
  • I think understand what you mean Andrew(And thanks to you other people as well!) By saying guess is equal to ten I'm not saying the value of the var is 10 I'm saying the word guess is equal to the number ten. Is that what you're saying? – OblongKilm Nov 17 '16 at 04:13
  • 1
    @OblongKilm Yes. In your current code you are ultimately doing this: `"10" != 10` which would actually give you `True` when your intention is that it should give you `False`. So if you *cast* it to int around your `raw_input`, then you are going to end up doing as you expect `10 != 10` which will now give you the expected `False` value. – idjaw Nov 17 '16 at 04:15

0 Answers0