-2

I am using python 2.7.9

I wrote this code below and when I run in the Shell the random number displays, I type it in and then nothing else happens. I don't get an error message but I don't see why it isn't printing "Good Guess"

import random

def guessNumber():  
    myNum = random.randint(1,1000)  
    print myNum  
    guess = raw_input("Guess my Number:")  
    if guess == myNum:  
        print "Good Guess" 
Joel
  • 22,598
  • 6
  • 69
  • 93
DaveCity
  • 3
  • 1
  • You never call anything, and `raw_input` always gives a string. – jonrsharpe Jan 08 '15 at 21:57
  • (I corrected your indenting. The problem was that to tell stackoverflow it's code you need to indent everything (including the `import random` and `def ...` ) – Joel Jan 08 '15 at 22:04
  • possible duplicate of [Python: Problem with raw\_input reading a number](http://stackoverflow.com/questions/5762938/python-problem-with-raw-input-reading-a-number) – Kevin Reid Jan 08 '15 at 22:20

1 Answers1

0

Your problem is that it's reading in the guess as a string. So it's comparing the string to an integer. That's always False. Change to

guess = int(raw_input("Guess my Number: "))

To make it robust

try:
    guess = int(raw_input("Guess my Number: "))
except ValueError:
    print "that wasn't an integer."
    continue

Then you can put a loop around this as you see fit.

Joel
  • 22,598
  • 6
  • 69
  • 93