I'm new to Python. Just started school for programming and have run into a bit of trouble with python.

seems every time i go to run module a little window will pop up labeled SyntaxError, and inside of the window it says invalid syntax. Not to sure what's going on, the code is fine. I have matched it to the website. I've uninstalled and reinstalled and have also repaired. I have enclosed a link to the image of the error. Thanks for any help.

LINK TO ERROR


-----------------


# This is a guess the number game.
import random

guessesTaken = 0

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')

while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
guess = input()
guess = int(guess)

guessesTaken = guessesTaken + 1

if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.

if guess > number:
print('Your guess is too high.')

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)

delete everything above "import random" in the picture you posted.

also use code tags when posting here it helps people run your code when they're testing it (press the code button and put

on the first code block. 

Instead of input() I would use 
[CODE=python]answer = raw_input('ask a question here')

other than that if your indents are correct I don't see anything else wrong. Instead of mutiple 'if' statements you could substitute 'elif', but that would just be for convention.

thank you for the reply. I have made the changes listed above and still getting that same error when I run module.

import random

guessesTaken = 0

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')

while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
answer = raw_input('ask a question here')
guess = int(guess)

guessesTaken = guessesTaken + 1

if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.

if guess > number:
print('Your guess is too high.')

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)

You have completely misindented the code and you are using input for inputting string, also all those str functions look ugly, take a look of % string formatting or format method for neat printing.

Do you use python 3 or 2?
By your print statement it look like you use python 3.

I dont want to reindented your code.
Here is a couple basic versions that may help you.

#python 2.x
import random

secret_number = random.randint(1,20)
counter = 0
guess = 0
print 'Guess the number between 1 and 20'
while guess != secret_number:
    guess = int(raw_input("Take a guess: "))
    if guess > secret_number:
        print 'Lower'
    elif guess < secret_number:
        print 'Higher'
    counter += 1
print 'You guessed it! The number was %s in %d tries' % (guess,counter)
#python 3.x
import random

secret_number = random.randint(1,20)
counter = 0
guess = 0
print('Guess the number between 1 and 20')
while guess != secret_number:
    guess = int(input("Take a guess: "))
    if guess > secret_number:
        print('Lower')
    elif guess < secret_number:
        print('Higher')
    counter += 1
print('You guessed it! The number was %s in %d tries' % (guess,counter))

Look at the difference in input() and print statement python 2 and 3,and i use string formatting.
There is also new string formatting it look like this,from python 2.6-->

print('You guessed it! The number was {0} in {1} tries'.format(guess,counter))

This new string formatting has a lot of power,so look into it.

Edit:looked into your syntax error link,so i se now that you use python 3.
But is good for you to learn the differnce python 2/3,most of the code books/tutorials/forum are python 2.x.

Edited 4 Years Ago by snippsat: n/a

This article has been dead for over six months. Start a new discussion instead.