Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Python Syntax Error. WHY?!

syntax syntax error

  • Please log in to reply
3 replies to this topic

#1 Waffles

Waffles

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 25 September 2010 - 06:49 PM

import random
again = "yes"
print "\tWelcome to Number Guess 1.2!"
print "\t     XXXX XXXXXX 2010"
while play_again == "yes":
    player_guess = False
    guess_count = 0
    difficulty = 0
    diff_pick = False
    player_name = raw_input("\nWhat is your name?: ")
    print "\nHello,", player_name,"this is a number guessing game!"
    print "\nPick a difficulty:\nEasy(10 guesses)\nMedium(6 guesses)\nHard(4 guesses)"
    diff_pick = raw_input("\nChoose: ")
    diff_pick = diff_pick.lower()
    if diff_pick != False:
        if diff_pick == "easy":
            difficulty = 10
        elif diff_pick == "medium":
            difficulty = 6
        elif diff_pick == "hard":
            difficulty = 2
    print "\nI am thinking of a number between 1 and 100"
    number = random.randrange(100) + 1
    while player_guess != number and guess_count != difficulty:
        player_guess = int(raw_input("Guess a number: "))
        if player_guess > number:
            print "Guess Lower..."
            guess_count += 1
        elif player_guess < number:
            print "Guess Higher..."
            guess_count += 1
    print "\nYEAH! You guessed right!"
    print "YOU WIN!"
    lplay_again = raw_input("Do you want to play again?\nYes or No: "
[COLOR="red"]else:[/COLOR]
    print "See Ya next time!"
raw_input("Press 'Enter' to quit.")

Its just a simple guessing game I wrote to show my buddy an example of Python.

The red text is the syntax error.

Why is this happening? :confused:
  • 0

#2 dbug

dbug

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 148 posts

Posted 26 September 2010 - 12:27 AM

while loop doesn't accept an else. I think you are missing an 'if' statement comparing the value of lplay_again with 'Yes', and that 'else' corresponds to this missing 'if' (and it must be more indented).
  • 0

#3 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 03 October 2010 - 03:14 PM

@dbug: That's incorrect, While loops in Python do accept else statements.

The reason your program is having a syntax error is because your raw_input function call does not have a closing parenthesis. Check it. :)

    lplay_again = raw_input("Do you want to play again?\nYes or No: "
Also, lplay_again will NOT change the contents of play_again!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 Waffles

Waffles

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 04 October 2010 - 02:45 PM

BAH!

So it WAS just a typo!

I looked over that block of code over and over and over, but I couldn't see the problem!

BAAAAAAAHHHH!
  • 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