2016-09-23 9 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に減らすための1回の変換のコストを支払うことができます(ただし、 '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

ときを'or'演算子を使用する場合は、条件の一部として使用します。たとえば :

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. 

2つ以上の条件のいずれかが真である必要がある場合は、 "or"演算子を使用する必要があります。しかし、あなただけの入力年齢がこの試す範囲内にあるかどうかを確認したい場合:

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ステートメントを必要としました –