-2

The first IF statement is being ignored and I have no idea what could cause this. I checked the indentation and everything seems fine.As you can see in the code it prints numberRolled but when I run it it justs ignores the first IF.`

import random
numberRolled = random.randint(1,6)
print numberRolled
while True:
    userGuess = raw_input("Guess a number\n")
    if userGuess == numberRolled:
        print "You got it right!"
        quitYN = raw_input("Would you like to play again?\n").lower()
        if quitYN == "yes":
            continue
        else:
            break
    elif userGuess != numberRolled:
        print "Wrong!"`
  • 5
    `numberRolled` is an integer, `userGuess` is a string. `raw_input("Guess a number\n")` => `int(raw_input("Guess a number\n"))`. Also your elif is redundant! It will only get to that point if they are not equal. You can just use else. – Paul Rooney Apr 27 '17 at 03:47
  • 1
    When if statements are "ignored" that means the condition isn't true. Run the code line by line and you'd see why – OneCricketeer Apr 27 '17 at 03:49
  • Thanks Pual Rooney, that worked. =) – Leon Goldner-Katz Apr 27 '17 at 03:51

1 Answers1

0

raw_input() returns a string, but random.randint() returns an int. This means that when doing userGuess == numberRolled you are comparing a string to an int (which returns False).

To fix this simply convert one of the variables to the correct type:

userGuess == str(numberRolled)

Take a look at this answer for more information about variable types and how to compare them in python.

mikelsr
  • 457
  • 6
  • 13