about syntax errors

Dwight Ivanov
Dwight Ivanov used Ask the Experts™
on
I'm practicing python and I'm using python 3.5 32-bit and created the "guessing game" program but continue to get a syntax error :
Here's my code-
import random
print("\tWelcome to 'Guess MY Number'!")
print("\n I'm thinking of a number between 1 and 100.")
print("Try to guess as little as possible.")

the_number = random.randint(1,100)
guess= True
Tries = 0

while guess !=the_number:
  guess = (input("Take a guess"))


  try:
      guess = int(guess)
  except VauleError:
        print("The was not an integer."
  else:
      if guess<1 or guess > 100:
          print("That was not a integer.")
        elif guess > the_number:
            print("Lower...")
        elif guess < the_number:
            print("Higher...")
  tries += 1

print("You guessed it! The number was", the_number)
print("And ou only took", tries,"tries.")

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most code, but especially python, must be inserted using code tags because then it is easier to see the indentation.
Based on your OP code, the indentation appears a little off. But maybe that was just the way you input your data in the OP.

You should copy/paste the exact syntax error into the OP.
Also, Tries and tries are different tokens. Case sensitive, Python is.
VauleError - typo.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

After correcting indentation, typos, and case sensitive mistakes, your program works fine.
Platform used: VS 2013 Community. Great debugger.
When observing a syntax error, it can be a bit higher than the reported line. You have a missing closing parenthesis at the line 17.

Use indentation by 4 spaces for one level. The elif... at line 21 and below are indented more that allowed with respect to the line 19. Here is a bit cleaned code:
#!python3

import random
print("\tWelcome to 'Guess MY Number'!")
print("\n I'm thinking of a number between 1 and 100.")
print("Try to guess as little as possible.")

the_number = random.randint(1, 100)
guess = None
tries = 0

while guess != the_number:
   guess = input("Take a guess: ")
   
   try:
       guess = int(guess)
   except ValueError:
       print("That was not an integer.")
   else:
       if guess < 1 or guess > 100:
           print("That was not a integer.")
       elif guess > the_number:
           print("Lower...")
       elif guess < the_number:
           print("Higher...")
   tries += 1

print("You guessed it! The number was", the_number)
print("And ou only took", tries,"tries.")

Open in new window

Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016
Commented:
Some more lines for you to look at that contain errors:

!=the_number:
VauleError:
print("The was not an integer."
guess<1
print("You guessed it! The number was", the_number)
print("And ou only took", tries,"tries.")

Author

Commented:
Thank you everyone
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
You're welcome. Since you're new at EE, don't forget to close the question ;)
You should add the "hash bang" sequence to the very first line like:
#!/usr/bin/env python3

Open in new window

Say the script is named guess_no, then use the command
chmod +x guess_no

Open in new window

The two things together will make your script an executable -- as any other executable utility. That is, you can launch it by simply typing
$ guess_no

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial