我的问题在第13行(否则)。 我收到错误“语法无效”
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")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")最满意答案
你的问题是缩进。 if必须与else 。 你似乎还有一个前所未有的领先空间。
if Guess < Answer: print("Too low! try again") else: print("too high")应该
if Guess < Answer: print("Too low! try again") else: print("too high")Gender == 'boy' or 'Boy'不符合您的期望。 由于Boy评估为true,它将等同于Gender == 'boy' 。 你可能想要Gender == 'boy' or Gender == 'Boy' ,如果你可以接受任何案件,可以简化为Gender.lower() == 'boy' 。
您可能还打算在while循环之前和while循环中阅读答案。
您还应该遵循接受的Python样式指南,并使用由下划线分隔的小写单词作为变量名称,例如gender而不是Gender 。 将Gender用于类名。
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.
更多推荐
发布评论