Python 3.1中的整数问题(Problem with an integer in Python 3.1)

编程入门 行业动态 更新时间:2024-10-23 11:24:00
Python 3.1中的整数问题(Problem with an integer in Python 3.1)

我的问题在第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.

更多推荐

本文发布于:2023-08-07 08:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   Python   Problem   integer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!