2016-06-25 8 views
-3

これはちょっとしたゲームですが、user_livesまたはcomp_livesのどちらかが0の場合、ループは終了せず、理由もわかりません。Python - なぜループ終了プログラム中にしませんか?

私はwhileループを設定した場合、それは動作します:user_lives> 1またはcomp_lives> 1ながら:

は私も両方ELIF文の文が、その後、私はちょうど必要がある場合で休憩を入れて、それを修正することができますwhileをTrueに設定し、無限ループを使用することができます。これは動作しますが、なぜこのwhileループが機能しないのかを知りたいと思います。

助けがあれば助かります。

from random import randint 

user_lives = 3 
comp_lives = 3 

print("Welcome to the number guessing game!!!") 
print("The game will choose a number between 0 and 10 and you must guess right before the computer does!!!") 

while user_lives > 0 or comp_lives > 0: 
    random_num = randint(0, 10) 
    user_num = raw_input("Choose a number between 0 and 10: ") 
    comp_num = randint(0, 10) 
    print(random_num) 
    print(comp_num) 

    if int(user_num) == random_num and comp_num == random_num: 
     print("Its a draw.") 

    elif comp_num == random_num: 
     print ("The computer has WON!") 
     user_lives = user_lives - 1 
     print("You have " + str(user_lives) + " lives left.") 
     if user_lives == 0: 
      print("GAME OVER.")   

    elif int(user_num) == random_num: 
     print("You have WON!") 
     comp_lives = comp_lives - 1 
     print("The computer has " + str(comp_lives) + " lives left.") 
     if comp_lives == 0: 
      print("GAME OVER.") 

    else: 
     print("No one wins. Try again.") 
+1

だろうと思いますか? – mid

+2

あなたのループは、*両方のプレイヤーが死ぬまで終了しません! :-) –

+0

あなたはVincentのソリューションをチェックしましたか?それはあなたの問題に対する解決策です。 –

答えて

2

次のようにあなたが投稿したコードは、ロジックを持っています

user_lives> 0の場合、ループを継続します。 OR IF comp_lives> 0の場合は、ループを続けます。

問題が表示されますか?ループは、usercompの両方が<=0の場合にのみ終了できます。

私はそのAN ORの代わりのため、おそらくより良いループが

while user_lives > 0 and comp_lives > 0:

+0

'if ...'ではなく 'while ...'を意味しませんか? –

+0

が修正されました。おかげです。 –

+0

意味があります、歓声。 – MWeasel

関連する問題