Help with Python number guessing game

im getting a syntax error that i need some help with

Page 1 of 1

8 Replies - 13742 Views - Last Post: 21 February 2010 - 11:24 PM Rate Topic: -----

#1 Andrew.G  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?"
userName = raw_input()
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  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • 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?"
userName = raw_input()
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"


Was This Post Helpful? 0
  • +
  • -

#3 Andrew.G  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Andrew.G  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • 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?"
userName = raw_input()
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.
Was This Post Helpful? 1

#7 Andrew.G  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Taylor*


Reputation:

Re: Help with Python number guessing game

Posted 21 February 2010 - 07:38 PM

View Postatik97, on 21 June 2009 - 06:50 PM, said:

This code run successfully-
import random
print "Hello, what is your name?"
userName = raw_input()
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.

Was This Post Helpful? 0

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1