2017-10-20 121 views
-4

我一直在爲岩石,紙張,剪刀遊戲編寫代碼。我必須跟蹤用戶的勝利,損失和吸引力。我遇到了一個問題,變量獲勝,失敗和抽籤被重置爲零。該函數的第二部分執行相同的操作,但將相應的變量賦值爲1.製作計數器的Python

def user_choice(): 
    print("Choice must be Rock, Paper, or Scissors.") 
    users_choice = input("Enter your choice here: ") 

    if users_choice == "Rock": 
     print("You chose Rock") 
     player_choice = "Rock" 
     computer_choice(player_choice) 
    elif users_choice == "Paper": 
     print("You chose Paper") 
     player_choice = "Paper" 
     computer_choice(player_choice) 
    elif users_choice == "Scissors": 
     print("You chose Scissors") 
     player_choice = "Scissors" 
     computer_choice(player_choice) 
    else: 
     print("Error: Choice must be one of the following: Rock, Paper, or Scissors") 
     user_choice() 


def computer_choice(player_choice): 
    #This is the program's randomly generated number, from here on out the programm will be refered to as Computer. 
    computer_generated_number = random.randint(1, 3) 
    player_choice = player_choice 

    if computer_generated_number == 1: 
     computer_choice = "Rock" 
     game(player_choice , computer_choice) 
    elif computer_generated_number == 2: 
     computer_choice = "Paper" 
     game(player_choice , computer_choice) 
    elif computer_generated_number == 3: 
     computer_choice = "Scissors" 
     game(player_choice , computer_choice) 


def game(player_choice , computer_choice): 
    wins = 0 
    losses = 0 
    draws = 0 

    if wins or losses or draws >= 1: 
     if player_choice == computer_choice: 
      print("It's a draw!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      draws = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Rock" and computer_choice == "Scissors": 
      print("Rock smashes scissors!") 
      print("You won!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      wins = wins + 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Paper" and computer_choice == "Rock": 
      print("Paper covers rock!") 
      print("You won!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      wins = wins + 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Scissors" and computer_choice == "Paper": 
      print("Scissors cut paper!") 
      print("You won!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      wins = wins + 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Rock" and computer_choice == "Paper": 
      print("Paper covers rock!") 
      print("You lost") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      losses = losses + 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Paper" and computer_choice == "Scissors": 
      print("Scissors cuts paper!") 
      print("You lost") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      losses = losses + 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Scissors" and computer_choice == "Rock": 
      print("Rock smashes scissors!") 
      print("You lost") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      losses = losses + 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

    if wins or losses or draws == 0: 
     if player_choice == computer_choice: 
      print("It's a draw!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      draws = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Rock" and computer_choice == "Scissors": 
      print("Rock smashes scissors!") 
      print("You won!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      wins = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Paper" and computer_choice == "Rock": 
      print("Paper covers rock!") 
      print("You won!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      wins = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Scissors" and computer_choice == "Paper": 
      print("Scissors cut paper!") 
      print("You won!") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      wins = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Rock" and computer_choice == "Paper": 
      print("Paper covers rock!") 
      print("You lost") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      losses = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Paper" and computer_choice == "Scissors": 
      print("Scissors cuts paper!") 
      print("You lost") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      losses = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 

     elif player_choice == "Scissors" and computer_choice == "Rock": 
      print("Rock smashes scissors!") 
      print("You lost") 
      user_input = input("Would you like to play again? Answer must be a yes or no. ") 
      losses = 1 

      if user_input == "Yes" or user_input == "yes": 
       user_choice() 
      elif user_input == "No" or user_input == "no": 
       print("You've won" , wins , "times") 
       print("You've lost" , losses , "times") 
       print("And you've come to a draw" , draws, "times") 
       print("Goodbye") 
+0

你做了什麼來調試呢? – astidham2003

+0

您是否試圖多次運行此功能? – BenT

+0

我嘗試過使用while語句,並且試圖在初始分配後能夠環繞變量 –

回答

0

我會建議一個簡化的「體系結構」。用於獲取用戶和計算機選擇的函數應該返回可以進行比較的值。

import random 

CHOICES = ('rock', 'paper', 'scissors') 

def get_user_choice(): 
    choice = input('Rock, paper, or scissors? ') 
    choice = choice.lower().strip() 
    if choice not in CHOICES: 
     print('Please select one of rock, paper, or scissors') 
     choice = get_user_choice() 
    return choice 

def get_computer_choice(): 
    return random.choice(CHOICES) 

def play_round(): 
    user_choice = get_user_choice() 
    computer_choice = get_computer_choice() 
    print('You selected', user_choice) 
    print('Computer selected', computer_choice) 
    # Compare choices here 
    # Return value indicating win, loss, or draw 

def play(): 
    wins = 0 
    losses = 0 
    draws = 0 
    result = play_round() 
    # Update wins, losses, or draws according to the result 
    # Prompt user to play again or quit 
    return wins, losses, draws 

if __name__ == '__main__': 
    wins, losses, draws = play() 
    # Report wins, losses, and draws