-1

Note: I'm using Python 2.7

I'm not very experienced at Python, but I decided to make a small simple program. Here is the code:

import random

while True:
    randomNumber = random.randrange(1, 3)
    print randomNumber
    guessedNumber = raw_input("Choose a number between 1 and 3 ")
    if randomNumber == guessedNumber:
        print 'Yay! You got it right!'
    else:
        print 'You got it wrong :( The number was:',randomNumber
#The first print is just for testing.

But when I try to run it I get this: IDLE after i used the program a few times

Can someone tell me what i need to change or what is wrong with the code?

Kellan
  • 3
  • 2
  • 1
    Note: "1" != 1. In Python 2 you could use `input` rather than `raw_input` (which always returns a string). – John Coleman Aug 20 '16 at 14:10
  • 1
    Possible duplicate of [How can I read inputs as integers in Python?](http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python) – DeepSpace Aug 20 '16 at 14:33
  • @JohnColeman `int(raw_input())` is usually considered a better approach as it won't try to evaluate the input which gives you a chance to validate it. – DeepSpace Aug 20 '16 at 14:35

1 Answers1

0

raw_input returns a string to guessedNumber, and your program compares a string (guessedNumber) to an integer (randomNumber), so if randomNumber == guessedNumber never evaluates to True.

The solution is to convert guessedNumber to an int and then compare the two values.

melonccoli
  • 398
  • 1
  • 4
  • 14