Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Weird syntax error

syntax syntax error

  • Please log in to reply
6 replies to this topic

#1 Andrew.G

Andrew.G

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 256 posts

Posted 21 June 2009 - 05:49 PM

Hey guys, im getting a syntax error on the "your guess is too high string" but i dont get it
Help is appreciated

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 anserw is too high'
elif guess < random:
    print 'Your anserw is too low'
elif guess == random:
    print 'Good job ' + userName + ', you guessed my number"
print 'The game is over"
P.S- this is a number guessing game
  • 0

#2 Hignar

Hignar

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 239 posts

Posted 22 June 2009 - 12:09 AM

You're mixing your quotation marks.

The line
print 'Good job ' + userName + ', you guessed my number"

should be
print 'Good job ' + userName + ', you guessed my number'

There is a similar problem with the last line as well.

Are you planning on making the game loop until the right number is guessed?
  • 0

#3 psam

psam

    CC Regular

  • New Member
  • PipPipPip
  • 35 posts

Posted 22 June 2009 - 02:22 AM

You should use input() instead of int(raw_input()).
And if the person doesn't guess the number at first then he'll never find out the number you thought. So you should add an while loop.

The code should be something like this:

from random import randint
userName = raw_input("What's your name ?")
print 'Hi' + userName + ', I am thinking of a number between 1 and 20, can you guess what it is?'
random = randint(1,20)
guess = input('Take a guess')
while guess != random:
    if guess > random:
        print 'Your guess was too high.'
    else:
        print 'Your guess was too low.'
    guess = input('Take a guess')
print 'Your guess was right.'

  • 0

#4 Andrew.G

Andrew.G

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 256 posts

Posted 30 June 2009 - 01:41 PM

import random, time

number = random.randint(1,10)
guessesTaken = 0

print 'Hello, what is your name?'
userName = raw_input()

print 'Hi ' + userName + ', I am thinking of a number between 1 and 10, can you guess what it is?I will give you 3 tries'

while guessesTaken < 3:
    guess = input('take a guess ')
    guessesTaken = guessesTaken + 1
    if guess > number:
        print 'your guess was too high'
    if guess < number:
        print 'your guess was too low'
    if number == guess:
        break

if guess == number:
    guessesTaken = str(guessesTaken)
    print 'Good job ' + userName + ', you got it in ' + guessesTaken + ' tries'

if guess != number:
    number = str(number)
    print 'Sorry ' + userName + ', but the number was ' + number


Better?
  • 0

#5 Andrew.G

Andrew.G

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 256 posts

Posted 01 July 2009 - 01:04 PM

How would I go about making an option to play again?
  • 0

#6 psam

psam

    CC Regular

  • New Member
  • PipPipPip
  • 35 posts

Posted 03 July 2009 - 05:39 AM

You can simply add another while loop.
Here's an example code:

import random

print 'Hello, what is your name?'
userName = raw_input()

loop = 0
while loop == 0:
    number = random.randint(1,10)
    print 'Hi ' + userName + ', I am thinking of a number between 1 and 10, can you guess what it is?I will give you 3 tries'
    guessesTaken = 0
    
    while guessesTaken < 3:
        guess = input('take a guess ')
        guessesTaken = guessesTaken + 1
        if guess > number:
            print 'your guess was too high'
        elif guess < number:
            print 'your guess was too low'
        elif number == guess:
            break

    if guess == number:
        guessesTaken = str(guessesTaken)
        print 'Good job ' + userName + ', you got it in ' + guessesTaken + ' tries'

    elif guess != number:
        number = str(number)
        print 'Sorry ' + userName + ', but the number was ' + number

    start = raw_input('Do you wish to play again? (y/n)')
    if start != 'y':
        loop = 1

Why did you add the time module ?
  • 0

#7 Andrew.G

Andrew.G

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 256 posts

Posted 03 July 2009 - 09:05 AM

Posted via CodeCall Mobile I was just messing with some time functions and forgot to take it out, but thanks for the help
  • 0





Also tagged with one or more of these keywords: syntax, syntax error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download