2016-06-23 28 views
-1

私はロックペーパーのはさみコードを2人で再生する必要がありますが、whileループを使用しようとすると、回答。ロックペーパーはさみコードでwhileループを使用すると、それ自体が繰り返されます

コード:

play = input("Do you want to play?") 
player1 = input("Rock, paper or scissors?") 
player2 = input("Rock, paper or scissors?") 
while play == "no": 
    print("Why are you wasting my time? HUH?") 
while play == "yes": 

    if (player1 == 'rock' and player2 == 'scissors'): 
     print ("Player 1 wins.") 
    elif (player1 == 'rock' and player2 == 'rock'): 
     print ("Tie") 
    elif (player1 == 'scissors' and player2 == 'paper'): 
     print ("Player 1 wins.") 
    elif (player2 == 'scissors' and player2 == 'scissors'): 
     print ("Tie") 
    elif (player1 == 'paper' and player2 == 'paper'): 
     print ("Tie") 
    elif (player1 == 'paper' and player2 == 'scissors'): 
     print ("Player 2 wins.") 
    elif (player1 == 'rock'and player2 == 'paper'): 
     print ("Player 2 wins.") 
    elif (player1 == 'paper' and player2 == 'rock'): 
     print ("Player 2 wins.") 
    elif (player1 == 'scissors' and player2 == 'rock'): 
     print ("Player 2 wins.")  
    else: 
     print ("This is not a valid object selection.") 
+4

「それは何ですか」... – tkausl

+0

そして、代わりに何をしたいですか? – interjay

+0

それは繰り返されません、そして、私はあなたが再びプレイするオプションを持っているので、ゲームが終了した後に再度尋ねるようにしたかった。 – Fox

答えて

1

問題は、あなたがいずれかのループに入った後playは、決して変わらないということです。したがって、ループは永遠に繰り返されます。 inputもループ内に移動する必要があります。

私はコードを少しwhileループを使って少し手直ししました。それは、プレイヤーが再生を止めたい場合、breakを使って取り消されます。それ以外の場合は、両方のプレイヤーに移動が要求され、その後に出力が印刷されます。その後、whileループはすべてを繰り返します。

また、else文をif文に追加しました。これは、再生するかどうかを尋ねられたときにyesかnoのどちらにも答えられない場合です。それから私はエラーメッセージを出力し、再度ループします。

while True: 
    play = input("Do you want to play?") 
    if play == "no": 
     print("Why are you wasting my time? HUH?") 
     break # exits the loop if you don't want to play 
    elif play == "yes": 
     player1 = input("Rock, paper or scissors?") 
     player2 = input("Rock, paper or scissors?") 
     if (player1 == 'rock' and player2 == 'scissors'): 
      print ("Player 1 wins.") 
     elif (player1 == 'rock' and player2 == 'rock'): 
      print ("Tie") 
     elif (player1 == 'scissors' and player2 == 'paper'): 
      print ("Player 1 wins.") 
     elif (player2 == 'scissors' and player2 == 'scissors'): 
      print ("Tie") 
     elif (player1 == 'paper' and player2 == 'paper'): 
      print ("Tie") 
     elif (player1 == 'paper' and player2 == 'scissors'): 
      print ("Player 2 wins.") 
     elif (player1 == 'rock'and player2 == 'paper'): 
      print ("Player 2 wins.") 
     elif (player1 == 'paper' and player2 == 'rock'): 
      print ("Player 2 wins.") 
     elif (player1 == 'scissors' and player2 == 'rock'): 
      print ("Player 2 wins.")  
     else: 
      print ("This is not a valid object selection.") 
    else: 
     print("What?") 
1

whileあなたがブレーク条件を与えるまでループの内側にあるものは何でも繰り返されます。あなたのケースでは、2つのwhileループをifの条件に置き換えて、コマンドラインを迷惑にしないようにすることができます。

後で別のゲームを自動的に要求したい場合は、ゲームから方法を作り、別のゲームの入力を求める別のユーザーの入力を促すことができます。

def game(): 
     if (player1 == 'rock' and player2 == 'scissors'): 
      print ("Player 1 wins.") 
     elif (player1 == 'rock' and player2 == 'rock'): 
      print ("Tie") 
     elif (player1 == 'scissors' and player2 == 'paper'): 
      print ("Player 1 wins.") 
     elif (player2 == 'scissors' and player2 == 'scissors'): 
      print ("Tie") 
     elif (player1 == 'paper' and player2 == 'paper'): 
      print ("Tie") 
     elif (player1 == 'paper' and player2 == 'scissors'): 
      print ("Player 2 wins.") 
     elif (player1 == 'rock'and player2 == 'paper'): 
      print ("Player 2 wins.") 
     elif (player1 == 'paper' and player2 == 'rock'): 
      print ("Player 2 wins.") 
     elif (player1 == 'scissors' and player2 == 'rock'): 
      print ("Player 2 wins.") 
     else: 
      print ("This is not a valid object selection.") 

play = input("Do you want to play?") 
player1 = input("Rock, paper or scissors?") 
player2 = input("Rock, paper or scissors?") 
if play == "no": 
    print("Why are you wasting my time? HUH?") 
elif play == "yes": 
    game() 
    another = input("Want to play again?") 
    if another == "yes": 
     game() 
    else: 
     print("Okay bye then.") 
+0

お返事ありがとう、本当に助けました:) – Fox

+0

よろしくお願いします! – Ian

関連する問題