1

My problem is in line 13(else). I get the error "invalid syntax"

Answer = 23
Guess = ()
Gender = input("Are you a boy, a girl or an alien? ")

if Gender == 'boy' or 'Boy':
     print("Nice!", Gender)
if Gender == 'girl' or 'Girl':
     print("Prepare do die!", Gender)
if Gender == 'alien' or 'Alien':
     print("AWESOME my", Gender, "Friend!")   
 while 'Guess' != Answer:
if Guess < Answer:
     print("Too low! try again")
    else:
        print("too high")
5
  • 2
    You're missing the part there you actually ask the user for a number. Read up on how to read console input. Commented Jan 17, 2011 at 21:27
  • There are actually lots of problems with this script. But what's the exact problem you are looking at? Commented Jan 17, 2011 at 21:27
  • Im new to programming so i dont know much about it. But this is supposed to be a guessing game. I get the error(invalid syntax) on "else". im not sure how im supposed to understand that :( Commented Jan 17, 2011 at 21:32
  • Fix the indentation on the print statement prior to the else. It should 3 more spaces. This is to match the indentation of the last print statement. Also, watch out for the indentation in the previous lines with while and if. Commented Jan 17, 2011 at 21:36
  • @dan04: an answer to your question is important, we should know whether to welcome our galactic overlords or overladies :) Commented Feb 11, 2011 at 16:29

3 Answers 3

4

Your problem is indentation. The if has to line-up with the else. You also seem to have a leading space before the while which must go.

if Guess < Answer:
     print("Too low! try again")
    else:
        print("too high")

should be

if Guess < Answer:
    print("Too low! try again")
else:
    print("too high")

Gender == 'boy' or 'Boy' doesn't do what you expect. Since Boy evaluates to true, it will be equivalent to just Gender == 'boy'. You probably want Gender == 'boy' or Gender == 'Boy', which can be simplified to Gender.lower() == 'boy' if you're okay accepting any case.

You probably also meant to read in the answer before and in the while loop.

You should also follow the accepted Python style guide and use lower-case words separated by underscores for your variable names, e.g. gender instead of Gender. Use Gender for class names.

Sign up to request clarification or add additional context in comments.

Comments

0

That code has serious problems with using or and with the difference between strings and variables. Is this what you want?:

Answer = 23
Guess = None
Gender = raw_input("Are you a boy, a girl or an alien? ")

if Gender in ('boy', 'Boy'):
     print("Nice!", Gender)
elif Gender in ('girl', 'Girl'):
     print("Prepare do die! %s" % Gender)
elif Gender in ('alien', 'Alien'):
     print("AWESOME my %s Friend!" % Gender)
while Guess != Answer:
    Guess = raw_input('Guess the number: ')
    try:
       Guess = int(Guess)
    except ValueError:
        print('Not an integer')
        continue
    if Guess == Answer:
        print('Alright!')
        break
    elif Guess < Answer:
        print("Too low! try again")
    else:
        print("too high")

Comments

0

Here is a (almost) correct program, with my comments:

# The recommended style for Python is to use CamelCase for classes only:
answer = 23 
guess = None # An empty tuple () works to, but this makes more sense.
gender = input("Are you a boy, a girl or an alien? ")

# Using gender.lower() means both 'Boy', 'boy', 'BOY' or 'boY' matches:
if gender.lower() == 'boy':
    print("Nice!", gender)
# Although you can do it like this too:
if gender in ('girl' or 'Girl'):
    print("Prepare do die!", gender)
# But this is *always* true, so it's wrong. I left this bug in intentionally:
if gender == 'alien' or 'Alien': 
    print("AWESOME my", gender, "friend!")

# 'guess' == answer will always be false. Remove the quotes:
while guess != answer:
    # And you forgot to ask for the guess...
    guess = int(input("Guess my age? "))

    # Indentation matters in Python:
    if guess == answer:
        print("Yeah, correct!")
    elif guess < answer:
        print("Too low! try again")
    else:
        print("too high")

This results in the following:

Are you a boy, a girl or an alien? Why, yes, I am.
AWESOME my Why, yes, I am. friend!
Guess my age? 20
Too low! try again
Guess my age? 30
too high
Guess my age? q
Traceback (most recent call last):
  File "untitled-1.py", line 19, in <module>
    guess = int(input("Guess my age? "))
ValueError: invalid literal for int() with base 10: 'q'

As you see, verifying your input is a good idea. :) But that's the next step.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.