2017-03-18 4 views
0

私はこのゲームのこのコードをユーザ入力を使って記述しましたが、動作していないようです。これは私のコードです。Pythonプログラミング:入力変数が名前エラーを返しています

import random 

print ("Welcome to the hungry dog game.") 
print ("The aim of this game is to save the hungry dog. How long can you keep it alive for?") 


true = True 
false = False 
aliveordeadlist1 = [true, false] 
random.choice(aliveordeadlist1) 





feed1 = input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.") 
if feed1 == "no" and aliveordeadlist1 == false: 
    print ("Well you were wrong, he was hungry. Your dog died sorry. Better luck next time.") 

if feed1 == "yes" and aliveordeadlist1 == false: 
    print ("As I told you he wansn't hungry> You overfed him so hed died sorry. Better luck next time."); 
if feed1 == "no" and aliveordeadlist1 == true: 
    print ("Well done he wasn't hungry. He is still alive (for now...)"); 
if feed1 == "yes" and aliveordeadlist1 == true: 
    print ("Well done he actually was hungry so he would have died if you didn't feed him.He is still alive (for now...)"); 

私は貝にし、例えば、このコードを実行すると、種類ははい、それはこのエラーを返します。

Traceback (most recent call last): 
    File "/Users/mac/Documents/hungrydoggame.py", line 16, in <module> 
    feed1 = input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.") 
    File "<string>", line 1, in <module> 
NameError: name 'yes' is not defined 

私はこのエラーを受けていると私は修正するために何をすべき理由を、私はわかりませんよ誰にも何か提案はありますか?

+0

私のために働いてください。 –

+2

'raw_input()'を使用 –

答えて

0

私はエラーは表示されませんが、変数にrandom.choiceを設定してif文で参照する必要があります(aliveordeadlist1ではなく)。そうでない場合は、リストを比較しています「はい」または「いいえ」という単一の単語に変換すると、それは常にFalseを返します。

0

このコードは、私は、Pythonの一部のバージョンでは予期しない動作につながる可能性を想像するいくつかの深刻な問題があります。

  • インデントはPythonで関連性があります。最初の行の後にすべてのコードを本当にインデントしたのであれば、なぜあなたのコードがまったく実行されるのか理解できません。
  • TrueFalseのエイリアスを定義します。このでも問題ありませんが、truefalseが予約語であるかどうかはわかりません。あなたはどこにでもrandom.choice戻り値を保存していない

    も論理的な問題があります。 [True, False] == True(および== False)かどうかを効果的にチェックしています。あなたが使用しているのPythonインタプリタれ、指定されていない

    他の可能性のある問題。 printはPython 2の関数ですが、Python 2の関数ではありません。私はassert(whatever)assert whateverの両方がPython 2の少なくともいくつかのバージョンで動作することを知っていますが、私はprint()についてはわかりません。

  • セミコロンは、Pythonでステートメントを区切るためには使用されません。すべての
1

まずあなたがここに記載input methodがそう、それはあなたが犬がある場合のために、ランダムな選択を行う文字列として及びline7で応答を返しますinput_rawとして使用されるべきであるpython2.7プログラムをやっている場合死んだら生きているが、決してあなたがチェクシングする変数にそれを保存しないでくださいaliveordead = random.choice(aliveordeadlist1)。だから適切なコードがあります。

import random 

print ("Welcome to the hungry dog game.") 
print ("The aim of this game is to save the hungry dog. How long can you keep it alive for?") 


true = True 
false = False 
aliveordeadlist1 = [true, false] 
aliveordead = random.choice(aliveordeadlist1) 





feed1 = raw_input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.") 

if feed1 == "no" and aliveordead == false: 
    print ("Well you were wrong, he was hungry. Your dog died sorry. Better luck next time.") 

if feed1 == "yes" and aliveordead == false: 
    print ("As I told you he wansn't hungry> You overfed him so hed died sorry. Better luck next time."); 
if feed1 == "no" and aliveordead == true: 
    print ("Well done he wasn't hungry. He is still alive (for now...)"); 
if feed1 == "yes" and aliveordead == true: 
    print ("Well done he actually was hungry so he would have died if you didn't feed him.He is still alive (for now...)"); 
関連する問題