或语句无效的语法

时间:2016-09-23 03:54:25

标签: python python-3.x

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

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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