【问题标题】:or statement invalid syntax或语句无效语法
【发布时间】:2016-09-23 12:21:11
【问题描述】:

所以,我的一个朋友告诉我 or 语句,但是当我使用它时,它说语法无效......但不会告诉我在哪里

#Fun Game

print("Enter name")

firstname = input()

print ("Thousands of years further from our time, the continents collided, creating mass devastation and heat.You,"+firstname+" lived in a peacful village until")

print (" your village was raided by four men, everyone else died other than you, who was trained as an assasin and killed them all") 

age1 = input ("you,"+firstname+" as a 16 year old killed them all and escaped into a cave. Now that cave is what you call home.Enter your character's current age(max 25 min 20) ")

if age1 == "20":
    print("")

or age1 == "21":
    print("")

or age1 == "22":
    print ("")

or age1 == "23":
    print ("")

or age1 == "24":
    print ("")

or age1 == "25":
    print("")

else:
    print ("Choose an age in the list")

cave1 = input ("You can do 2 things:")

print ("1.Go to your friend's village(700 people,military grade)and trade,look for bountys and find news")

【问题讨论】:

  • Python 总是告诉你错误在哪里。
  • 这次没做(你知道为什么吗?)
  • 你是在 IDLE 中运行这个吗?
  • 看看有没有错误,是的,然后我在CMD里运行
  • 可能有一个箭头指向or... 随心所欲地查看elif 而不是or

标签: python python-3.x


【解决方案1】:

Poonam 的答案看起来不错,但您可能也不想要那么多 if 语句:

print("Enter name")

firstname = input()

print ("Thousands of years further from our time, the continents collided, creating mass devastation and heat.You,"+firstname+" lived in a peacful village until")

print (" your village was raided by four men, everyone else died other than you, who was trained as an assasin and killed them all")

age1 = input ("you,"+firstname+" as a 16 year old killed them all and escaped into a cave. Now that cave is what you call home.Enter your character's current age(max 25 min 20) ")

if age1 in ["20","21","22","23","24","25"]:
  print("")
else:
  print ("Choose an age in the list")

cave1 = input ("You can do 2 things:")

print ("1.Go to your friend's village(700 people,military grade)and trade,look for bountys and find news")

【讨论】:

  • 是的,很好,回答
  • 如果你真的想验证它甚至是一个年龄(int),或者更好一点,你可以检查if 20 <= int(age) <= 25:,支付一次转换的成本以将检查从6减少到2(虽然如果它不能解释为int,则会引发异常;您可以捕获它或让它转储给用户并结束程序。
【解决方案2】:
print("Enter name")

firstname = input()

print ("Thousands of years further from our time, the continents collided, creating mass devastation and heat.You,"+firstname+" lived in a peacful village until")

print (" your village was raided by four men, everyone else died other than you, who was trained as an assasin and killed them all")

age1 = input ("you,"+firstname+" as a 16 year old killed them all and escaped into a cave. Now that cave is what you call home.Enter your character's current age(max 25 min 20) ")

if age1 == "20": print("")

elif  age1 == "21": print("")

elif age1 == "22": print ("")

elif age1 == "23": print ("")

elif age1 == "24": print ("")

elif age1 == "25": print("")

else: print ("Choose an age in the list")

cave1 = input ("You can do 2 things:")

print ("1.Go to your friend's village(700 people,military grade)and trade,look for bountys and find news")

【讨论】:

    【解决方案3】:

    当您使用“或”运算符时,您将其用作条件的一部分。 例如:

    if x == 1 or x == 5:
        print(x)
    

    所以你的代码将是一长行,没有所有的打印语句:

    if age1 == "20" or age1 == "21" or age1 == "22" or age1 == "23" or age1 == "24" or age1 == "25":
        print("")
    

    从您的代码中,我认为您想要的是一个 elif 语句:

    if age1 == "20":
        ##Do something based on input of 20
    elif age1 == "21":
        ##Do something else if input is 21
    elif age1 == "22":
        ##Something else again if input is 22
    else:
        ##Do something if the age is not captured above.
    

    如果您需要两个或多个条件之一为真,您只需要使用“或”运算符。但是,如果您只想检查输入的年龄是否在范围内,请尝试以下操作:

    if inval >= 20 and inval <= 25:
        print("Correct Value")
    else:
        print("Incorrect value")
    

    或者,使用范围:

    if inval in range(20, 26):
        print("Correct Value")
    else:
        print("Incorrect value")
    

    【讨论】:

    • 感谢您的快速回复,安德鲁原来是问题所在
    • 我需要 elif 语句
    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 2014-11-23
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    相关资源
    最近更新 更多