Closed Thread
Results 1 to 7 of 7

Thread: Weird syntax error

  1. #1
    Andrew.G's Avatar
    Andrew.G is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Woodstock, GA
    Posts
    396
    Rep Power
    0

    Weird syntax error

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

    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 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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Weird syntax error

    You're mixing your quotation marks.

    The line
    Code:
     print 'Good job ' + userName + ', you guessed my number"
    should be
    Code:
    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?

  4. #3
    psam is offline Learning Programmer
    Join Date
    Jun 2009
    Posts
    34
    Rep Power
    10

    Re: Weird syntax error

    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:

    Code:
    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.'

  5. #4
    Andrew.G's Avatar
    Andrew.G is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Woodstock, GA
    Posts
    396
    Rep Power
    0

    Re: Weird syntax error

    Code:
    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?

  6. #5
    Andrew.G's Avatar
    Andrew.G is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Woodstock, GA
    Posts
    396
    Rep Power
    0

    Re: Weird syntax error

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

  7. #6
    psam is offline Learning Programmer
    Join Date
    Jun 2009
    Posts
    34
    Rep Power
    10

    Re: Weird syntax error

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

    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 ?

  8. #7
    Andrew.G's Avatar
    Andrew.G is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Woodstock, GA
    Posts
    396
    Rep Power
    0

    Re: Weird syntax error

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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Weird Compile Error (C)
    By breimer273 in forum C and C++
    Replies: 3
    Last Post: 03-15-2011, 05:45 AM
  2. Weird STL map error
    By dargueta in forum C and C++
    Replies: 8
    Last Post: 03-09-2011, 06:03 PM
  3. [help]Parse error: syntax error, unexpected $end
    By kiddies in forum PHP Development
    Replies: 3
    Last Post: 07-18-2010, 02:58 PM
  4. Weird error
    By julmuri in forum C and C++
    Replies: 6
    Last Post: 01-28-2009, 11:40 AM
  5. weird error in my opinion
    By Whitey in forum PHP Development
    Replies: 5
    Last Post: 03-31-2008, 09:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts