2016-11-15 7 views
0

私は、プレイしたいラウンドの数を入力するようにユーザーに求めましたが、実際には別のラウンドでプログラムをプレイする方法はわかりません。コードの終わり近くで、私はスコアをラウンドに加えるようにしようとしています。どんな助け?ロック、ペーパー、ハサミのゲームをPythonで作成する。

import time 
import random 
print ("Welcome to the Rock, Paper, Scissors Game!") 
time.sleep(1) 
rounds = int(raw_input("How many rounds would you like to play?")) 
while rounds not in range(3,11): 
    try: 
     rounds = int(raw_input("Sorry! I can only play between 3 and 10 rounds. Try again:")) 
    except: 
     print ("ERROR invalid input. Out of range or wrong type of data.") 
if rounds in range(3,11): 
    time.sleep(1) 
    print ("Challenge accepted! I will play you for", rounds,"rounds! Let's begin!!!") 
options = ("rock","paper","scissors") 
userChoice = raw_input("Rock, Paper or Scissors, peasant?") 
while userChoice not in options: 
    try: 
     userChoice = raw_input("Not an option lad. Rock, paper or scissors") 
    except: 
     print ("ERROR invalid input. Out of range or wrong type of data.") 
cpuoption1 = "rock" 
cpuoption2 = "paper" 
cpuoption3 = "scissors" 
cpuChoice = random.randint(1,3) 
answer = "" 
if cpuChoice == 1: 
    answer = cpuoption1 
elif cpuChoice == 2: 
    answer = cpuoption2 
elif cpuChoice == 3: 
    answer = cpuoption3 
print ("I choose", answer,"!") 
user= 0 
cpu = 0 
if userChoice == "rock" and answer == cpuoption1: 
    user += 1 
    cpu += 1 
    print ("Draw!") 
elif userChoice == "paper" and answer == cpuoption2: 
    user+=1 
    cpu+=1 
    print("Draw!") 
elif userChoice == "scissors" and answer == cpuoption3: 
    user+=1 
    cpu+=1 
    print ("Draw!") 
elif userChoice == "rock" and answer == cpuoption2: 
    cpu+=1 
    print ("CPU wins the round!") 
elif userChoice == "rock" and answer == cpuoption3: 
    user+=1 
    print ("Player wins the round!") 
elif userChoice == "paper" and answer == cpuoption1: 
    user+=1 
    print ("Player wins the round!") 
elif userChoice == "paper" and answer == cpuoption3: 
    cpu+=1 
    print ("CPU wins the round!") 
elif userChoice == "scissors" and answer == cpuoption1: 
    cpu+=1 
    print ("CPU wins the round!") 
elif userChoice == "scissors" and answer == cpuoption2: 
    user+=1 
    print ("Player wins the round!") 
if cpu==rounds: 
    print("CPU beat the player!") 
elif user==rounds: 
    print("Player beat the CPU!") 
elif cpu==rounds and user==rounds: 
    print("Looks like that was a draw! Good game!") 

答えて

0

LOOP別のゲーム私が今見る

keep_going = True 
while keep_going: 
    # Here, insert all of your code to play one game 

    response = raw_input("Do you want to play another game? (Y or N)" 
    keep_going = (response == 'Y') 

を再生します。あなたは、あなたがプレーしたいラウンド数を事前に知っていますか?その場合には、ためのループを使用します。

if cpu > user: 
    print("CPU wins the match!") 
elif user > cpu: 
    print("Player wins the match!") 
else: 
    print("Looks like that was a draw! Good match!") 

:あなたはすべてのゲーム(ラウンド)で完了したら

for i in range(rounds): 
    # Here, insert all of your code to play one game 

を、あなたはより多くのようなものをお勧めしますより簡単に勝者を決定するコーディング:

choice_list = { "rock":1, "paper":2, "scissors":3} 
# input user choice; generate cpu choice as 1, 2, or 3 
result_diff = (choice_list[userChoice] - cpuChoice) % 3 
if result_diff == 0: 
    print ("Draw") 
elif result_diff == 1: 
    print ("User wins") 
    user += 1 
else: 
    print ("cpu wins") 
    cpu += 1 

それは論理です。私はあなたに会計の詳細を残します。

0

カウンターで行うことができます。

counter = 0 
while counter <= rounds: 
    #code here 
    counter += 1 #when the game is over 

編集:

import time 
import random 
print ("Welcome to the Rock, Paper, Scissors Game!") 
time.sleep(1) 
rounds = int(raw_input("How many rounds would you like to play?")) 
counter = 0 
while counter <= rounds-1: 
    while rounds not in range(3,11): 
     try: 
      rounds = int(raw_input("Sorry! I can only play between 3 and 10 rounds. Try again:")) 
     except: 
      print ("ERROR invalid input. Out of range or wrong type of data.") 
    if rounds in range(3,11): 
     time.sleep(1) 
     print ("Challenge accepted! I will play you for", rounds,"rounds! Let's begin!!!") 
    options = ("rock","paper","scissors") 
    userChoice = raw_input("Rock, Paper or Scissors, peasant?") 
    while userChoice not in options: 
     try: 
      userChoice = raw_input("Not an option lad. Rock, paper or scissors") 
     except: 
      print ("ERROR invalid input. Out of range or wrong type of data.") 
    cpuoption1 = "rock" 
    cpuoption2 = "paper" 
    cpuoption3 = "scissors" 
    cpuChoice = random.randint(1,3) 
    answer = "" 
    if cpuChoice == 1: 
     answer = cpuoption1 
    elif cpuChoice == 2: 
     answer = cpuoption2 
    elif cpuChoice == 3: 
     answer = cpuoption3 
    print ("I choose", answer,"!") 
    user= 0 
    cpu = 0 
    if userChoice == "rock" and answer == cpuoption1: 
     user += 1 
     cpu += 1 
     print ("Draw!") 
     counter += 1 
    elif userChoice == "paper" and answer == cpuoption2: 
     user+=1 
     cpu+=1 
     print("Draw!") 
     counter += 1 
    elif userChoice == "scissors" and answer == cpuoption3: 
     user+=1 
     cpu+=1 
     print ("Draw!") 
     counter += 1 
    elif userChoice == "rock" and answer == cpuoption2: 
     cpu+=1 
     print ("CPU wins the round!") 
     counter += 1 
    elif userChoice == "rock" and answer == cpuoption3: 
     user+=1 
     print ("Player wins the round!") 
     counter += 1 
    elif userChoice == "paper" and answer == cpuoption1: 
     user+=1 
     print ("Player wins the round!") 
     counter += 1 
    elif userChoice == "paper" and answer == cpuoption3: 
     cpu+=1 
     print ("CPU wins the round!") 
     counter += 1 
    elif userChoice == "scissors" and answer == cpuoption1: 
     cpu+=1 
     print ("CPU wins the round!") 
     counter += 1 
    elif userChoice == "scissors" and answer == cpuoption2: 
     user+=1 
     print ("Player wins the round!") 
     counter += 1 
    if cpu==rounds: 
     print("CPU beat the player!") 
    elif user==rounds: 
     print("Player beat the CPU!") 
     counter += 1 
    elif cpu==rounds and user==rounds: 
     print("Looks like that was a draw! Good game!") 
     counter += 1 
関連する問題