2017-11-27 10 views
0

ここは私の岩、紙、はさみのゲームですが、私は再びプレイ機能を追加したいと思いますが、どこにどのようにしたらいいのか分かりません。手伝ってくれてありがとう。再生コードを追加する場所はどこですか?

from random import randint 

player = input('rock, paper or scissors?') 
print(player, 'vs', end=' ') 

chosen = randint(1,3) 
#print(chosen) 

if chosen == 1: 
    computer = 'rock' 

elif chosen == 2: 
    computer = 'paper' 

else: 
    computer = 'scissors' 

print(computer) 

if player == computer: 
    print('DRAW') 

elif player == 'rock' and computer == 'paper': 
    print('COMPUTER WINS') 

elif player == 'rock' and computer == 'scissors': 
    print('YOU WIN') 

elif player == 'paper' and computer == 'scissors': 
    print('COMPUTER WINS') 

elif player == 'paper' and computer == 'rock': 
    print('YOU WIN') 

elif player == 'scissors' and computer == 'rock': 
    print('YOU WIN') 

elif player == 'scissors' and computer == 'paper': 
    print('COMPUTER WINS') 
+3

埋め込みコードを内のすべてをwhileループ – BadMiscuit

+0

すべてをwhileループに入れ、もう一度再生条件を追加してください:) – Abhijeetk431

答えて

0

私はグループに可能な結果を​​あなたをお勧めします、コードが短くするwhileループ

while True: 
    layer = input('rock, paper or scissors?') 
    print(player, 'vs', end=' ') 

    chosen = randint(1,3) 

    if chosen == 1: 
     computer = 'rock' 

    elif chosen == 2: 
     computer = 'paper' 

    else: 
     computer = 'scissors' 

    print(computer) 

    if player == computer: 
     print('DRAW') 

    elif player == 'rock' and computer == 'paper': 
     print('COMPUTER WINS') 

    elif player == 'rock' and computer == 'scissors': 
     print('YOU WIN') 

    elif player == 'paper' and computer == 'scissors': 
     print('COMPUTER WINS') 

    elif player == 'paper' and computer == 'rock': 
     print('YOU WIN') 

    elif player == 'scissors' and computer == 'rock': 
     print('YOU WIN') 

    elif player == 'scissors' and computer == 'paper': 
     print('COMPUTER WINS') 

でのラップすべて。

if player == computer: 
    print('DRAW') 
elif ((player == 'rock' and computer == 'paper') or 
    (player == 'paper' and computer == 'scissors') or 
    (player == 'scissors' and computer == 'rock')): 
    print('COMPUTER WINS') 
else: 
    print('YOU WIN') 
0

プレイヤーはまだあなたがwhileループを使用して、プレイヤーが再びプレーしたいかどうかを見ることができ

from random import randint 

playagain = 'y' 
while (playagain == 'y'): 
    player = input('rock, paper or scissors?') 
    print(player, 'vs', end=' ') 

    chosen = randint(1,3) 
    #print(chosen) 
    if chosen == 1: 
    computer = 'rock' 

    elif chosen == 2: 
    computer = 'paper' 

    else: 
    computer = 'scissors' 

    print(computer) 

    if player == computer: 
    print('DRAW') 

    elif player == 'rock' and computer == 'paper': 
    print('COMPUTER WINS') 

    elif player == 'rock' and computer == 'scissors': 
    print('YOU WIN') 

    elif player == 'paper' and computer == 'scissors': 
    print('COMPUTER WINS') 

    elif player == 'paper' and computer == 'rock': 
    print('YOU WIN') 

    elif player == 'scissors' and computer == 'rock': 
    print('YOU WIN') 

    elif player == 'scissors' and computer == 'paper': 
    print('COMPUTER WINS') 

    playagain = input('Play again ? y/n') 
0

をプレイしたいかどうかを確認するためのフラグを使用します。

from random import randint 

while True: 
    player = input('rock, paper or scissors?') 
    print(player, 'vs', end=' ') 

    chosen = randint(1,3) 
    #print(chosen) 

    if chosen == 1: 
     computer = 'rock' 

    elif chosen == 2: 
     computer = 'paper' 

    else: 
     computer = 'scissors' 

    print(computer) 

    if player == computer: 
     print('DRAW') 

    elif player == 'rock' and computer == 'paper': 
     print('COMPUTER WINS') 

    elif player == 'rock' and computer == 'scissors': 
     print('YOU WIN') 

    elif player == 'paper' and computer == 'scissors': 
     print('COMPUTER WINS') 

    elif player == 'paper' and computer == 'rock': 
     print('YOU WIN') 

    elif player == 'scissors' and computer == 'rock': 
     print('YOU WIN') 

    elif player == 'scissors' and computer == 'paper': 
     print('COMPUTER WINS') 

    play_again = input('Do you want to play again[yes/no]? ') 
    if play_again.lower() == 'no': 
     break 
関連する問題