2016-09-23 110 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語句 –