-1

I try to write a completely separate script based on what I just learned, to try and make sure I understand it. In the following code, I just cannot understand why, when guessing the correct random number, it never reaches my ELSE: statement, printing that the guess was correct? I know the answer has to be simple, but I am just confused.

from sys import exit
import random

print "Guess the number between 1 thru 10"
print "You have 3 tries"
guess = raw_input("Enter a guess-->")

tries = 3
random_number = random.randrange(1,10)
while tries > 1 and guess != random_number:
    print "That's not the number, try again!"
    print "Tries remaining:", tries - 1
    guess = raw_input("Next guess?-->")
    tries = tries - 1

if guess != random_number:
    print "Sorry, you lose!"
    print "The number was", random_number
    exit(0)

else:
    print "You guessed it!"
vaultah
  • 44,105
  • 12
  • 114
  • 143
MCgendraft
  • 21
  • 2
  • How is this a duplicate? I get that the differences between strings and integers are explained in the "duplicate" post, but as a person who survives as an IT professional due to forums, but is new to programming avoid a duplicating question, when I stated I was a beginner, have no error messages to follow, and unsure where to even research an answer, as I was not getting any information as to why the problem was occuring? I attempted line by line shell commands, and it appeared my variable was an integer after the raw_input command? – MCgendraft Nov 11 '15 at 09:26

1 Answers1

-2

You need to compare your guess as an int. raw_intput reads a line from input and converts it to a string.

from sys import exit
import random

print "Guess the number between 1 thru 10"
print "You have 3 tries"
guess = raw_input("Enter a guess-->")

tries = 3
random_number = random.randrange(1,10)
while tries > 1 and int(guess) != random_number:
    print "That's not the number, try again!"
    print "Tries remaining:", tries - 1
    guess = raw_input("Next guess?-->")
    tries = tries - 1

if int(guess) != random_number:
    print "Sorry, you lose!"
    print "The number was", random_number
    exit(0)

else:
    print "You guessed it!"
Avión
  • 7,963
  • 11
  • 64
  • 105
  • Why the downvote, sirs? – Avión Nov 11 '15 at 08:57
  • @vaultah Really? https://docs.python.org/2/library/functions.html#raw_input – Avión Nov 11 '15 at 08:59
  • Okay, removed the "incorrect" part. That said, I'd expect it to convert that line from bytes to string, not from `int` to string. I think that sentence is misleading. And you still convert `guess` to `int` twice. – vaultah Nov 11 '15 at 09:01
  • So do you know any better approach to archieve OP's goal without casting two times `guess` using the OP's code? – Avión Nov 11 '15 at 09:04
  • Yes. Convert `guess` to `int` once, as explained in the linked question. – vaultah Nov 11 '15 at 09:08
  • Try `guess = int(raw_input("Enter a guess-->"))` and you'll see it doesnt work. Why is this? I'm using Python 2.7 – Avión Nov 11 '15 at 09:11
  • 1
    I tried `guess = int(raw_input("Enter a guess-->"))` and `guess = int(raw_input("Next guess?-->"))` and it worked. Sorry, my previous comments were confusing. – vaultah Nov 11 '15 at 09:15
  • So it's better to cast *two times* the input, than casting two times the value in the condition? – Avión Nov 11 '15 at 09:20