2016-09-23 114 views
0

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

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

Python总是告诉你错误在哪里。 – techydesigner

+0

这次没有这样做(你知道为什么吗?) –

+0

你在IDLE中运行这个吗? – techydesigner

回答

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

是的,这是一个罚款,回答 –

+0

或者如果你真的想验证它甚至是一个年龄('int') ,你可以检查'if 20 <= int(age)<= 25:',支付一次转换的开销以将检查从6减少到2(尽管如果它不能被解释为int,这会增加一个例外;你可以捕捉它或让它转储给用户并结束程序)。 – ShadowRanger

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

谢谢,我会试试看看它是否有效 –

+0

作品!!!!!谢谢你,Poonam –

0

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

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

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

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

感谢您的快速回复,安德鲁事实证明,这是问题 –

+0

我需要elif语句 –