开发者

Problem with an integer in Python 3.1

开发者 https://www.devze.com 2023-02-05 00:06 出处:网络
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? \")

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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消