Help with Python number guessing game

Page 1 of 1

8 Replies - 19003 Views - Last Post: 21 February 2010 - 11:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=111078&amp;s=b2b668f870e6bffbb70b3c6ae1b35479&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Andrew.G

Reputation: 0
• Posts: 4
• Joined: 20-June 09

Help with Python number guessing game

Posted 20 June 2009 - 07:57 PM

```print "Hello, what is your name?"
print " Hi " + userName + ", I am thinking of a number between 1 and 20, can you guess what it is?"
import random
random = random.randomint(1,20)
print "take a guess"
guess = raw_input()
if (guess > random)
print "Your guess is too high"
elif (guess < random)
print "Your guess is too low"
elif (guess == random)
print "Good job " + userName + ", you guessed my number"

print "The game is over"
```

would somebody please tell me what i am doing wrong,
help is much appreciated
P.S- This is written in Python

This post has been edited by Andrew.G: 21 June 2009 - 06:38 AM

Is This A Good Question/Topic? 0

Replies To: Help with Python number guessing game

#2 atik97

• ???

Reputation: 144
• Posts: 715
• Joined: 16-September 08

Re: Help with Python number guessing game

Posted 21 June 2009 - 07:02 AM

Take a look at the below code-
```import random
print "Hello, what is your name?"
print " Hi " + userName + ", I am thinking of a number between 1 and 20, can you guess what it is?"
random = random.randint(1,20)
guess = int(raw_input("Take a guess:"))
if guess > random:
print "Your guess is too high"
elif guess < random:
print "Your guess is too low"
elif guess == random:
print "Good job " + userName + ", you guessed my number"
print "The game is over"

```

#3 Andrew.G

Reputation: 0
• Posts: 4
• Joined: 20-June 09

Re: Help with Python number guessing game

Posted 21 June 2009 - 12:44 PM

OK thanks, I'm guessing my problems were the parenthesis and the not defining the guess as an integer?

#4 atik97

• ???

Reputation: 144
• Posts: 715
• Joined: 16-September 08

Re: Help with Python number guessing game

Posted 21 June 2009 - 05:59 PM

Yes, there were error both in syntax of if statement and comparing a string with a number.

#5 Andrew.G

Reputation: 0
• Posts: 4
• Joined: 20-June 09

Re: Help with Python number guessing game

Posted 21 June 2009 - 06:40 PM

im getting a syntax error that highlights the "gues" part of guess in the "your guess is too high string", i think something is messed up with python

#6 atik97

• ???

Reputation: 144
• Posts: 715
• Joined: 16-September 08

Re: Help with Python number guessing game

Posted 21 June 2009 - 07:50 PM

This code run successfully-
```import random
print "Hello, what is your name?"
print " Hi " + userName + ", I am thinking of a number between 1 and 20, can you guess what it is?"
random = random.randint(1,20)
guess = int(raw_input("Take a guess:"))
if guess > random:
print "Your guess is too high"
elif guess < random:
print "Your guess is too low"
elif guess == random:
print "Good job " + userName + ", you guessed my number"
print "The game is over"

```

Check if any piece of code is changed or not. If you have modified your code, then resubmit it so i can review it.

#7 Andrew.G

Reputation: 0
• Posts: 4
• Joined: 20-June 09

Re: Help with Python number guessing game

Posted 21 June 2009 - 08:33 PM

ok, i ran it again and it worked,
thanks a bunch!

Reputation:

Re: Help with Python number guessing game

Posted 21 February 2010 - 07:38 PM

atik97, on 21 June 2009 - 06:50 PM, said:

This code run successfully-
```import random
print "Hello, what is your name?"
print " Hi " + userName + ", I am thinking of a number between 1 and 20, can you guess what it is?"
random = random.randint(1,20)
guess = int(raw_input("Take a guess:"))
if guess > random:
print "Your guess is too high"
elif guess < random:
print "Your guess is too low"
elif guess == random:
print "Good job " + userName + ", you guessed my number"
print "The game is over"

```

Check if any piece of code is changed or not. If you have modified your code, then resubmit it so i can review it.

#9 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

Re: Help with Python number guessing game

Posted 21 February 2010 - 11:24 PM

It's important to talk about the specific mistakes so that you can recognize them, rather than simply copying and pasting a corrected program.

First of all, when you tried to generate a random number, you used "random.randomint(1,20)" as you may have noticed, it should be "random.randint(1,20)". Also, after your conditional statements, you need a colon.

Lastly, when you use raw_input, you are given a string. Since we want to compare numbers, we need to cast (convert) your string to an int (whole number). Changing "raw_input()" to "int(raw_input())" fixed this. The reason is, we use raw_input to get the text from the user, and then we use int() to cast it to an int so that we can compare numbers to numbers.

I know you probably didn't need this, but I feel knowing what went wrong is going to serve you much more than simply knowing a fix for it.

This post has been edited by atraub: 21 February 2010 - 11:32 PM