2016-12-14 75 views
1

我需要一些幫助,我試圖完成這個程序。這件事情是一團糟。它是一個12小時的一天結束,今晚將到期,我認爲在這一點上我只會讓事情變得更糟。我正在努力實現player_choice的「4」輸入,並讓程序只是說'退出程序'並停止。當我輸入當前的選擇時,我得到我認爲是無止境的循環,因爲整個IDE鎖定並崩潰。得到無盡的循環

方案要求:

1) Get and return the value of the computer’s choice as an integer. 
2) Use a menu to get, validate, and return the player’s choices. 
The player will enter their menu choice for rock, paper, scissors, or quit. 
Validate the choice before returning it. 
3) Determine the winner. There are a total of 9 possible combinations of 
computer and player choices. The menu should always be displayed after the outcome has 
been displayed regardless of the outcome 
(i.e. ties should be treated the same as any other outcome). 
4) After the player chooses to stop playing, display the total number of 
games that ended in a tie, the total number the computer won, and the total 
number the player won. 

下面是程序代碼:

import random 

def main(): 
    display_menu() 
    print('There were', number_of_tied_games, 'tie games played.') 
    print('The computer won', number_of_computer_games, 'game(s).') 
    print('You won', number_of_player_games, 'game(s).') 

def process_computer_choice(): 
    choice1 = random.randint(1,3) 
    return choice1 

def process_player_choice(): 
    print('What is your choice?') 
    choice2 = (input()) 
    while choice2 != "1" and choice2 != "2" and choice2 != "3" and choice2 != "4": 
     print("ERROR: the choice can only be 1, 2, 3, or 4.") 
     choice2 = (input("Please enter a correct choice: ")) 
    return choice2 

def determine_winner(player_choice, computer_choice, choice2): 
    while choice2 != "4": 
     if computer_choice == 1: 
      if player_choice == "2": 
       print('Paper covers rock. You win!') 
       winner = 'player' 
      elif player_choice == "3": 
       print("Rock crushes scissors. The computer wins!") 
       winner = 'computer' 
      else: 
       print('The game is tied. Try again.') 
       winner = 'tied' 
     if computer_choice == 2: 
      if player_choice == "1": 
       print('Paper covers rock. The computer wins!') 
       winner = 'computer' 
      elif player_choice == "3": 
       print("Scissors cuts paper. You win!") 
       winner = 'player' 
      else: 
       print('The game is tied. Try again.') 
       winner = 'tied' 
     if computer_choice == 3: 
      if player_choice == "1": 
       print('Rock smashes scissors. You win!') 
       winner = 'player' 
      elif player_choice == "2": 
       print("Scissors cuts paper. The computer wins!") 
       winner = 'computer' 
      else: 
       print('The game is tied. Try again.') 
       winner = 'tied' 
    return winner 

def display_menu(): 
    choice2 = 0 
    number_of_tied_games = 0 
    number_of_player_games = 0 
    number_of_computer_games = 0 

    print('  MENU') 
    print('1) Rock') 
    print('2) Paper') 
    print('3) Scissors') 
    print('4) Quit') 
    print("Let's play the game of Rock, Paper, Scissors.") 
    computer_choice = process_computer_choice() 
    player_choice = process_player_choice()  
    while choice2 != "4": 

     if computer_choice == 1: 
      print('The computer chooses rock.') 
     elif computer_choice == 2: 
      print('The computer chooses paper.') 
     else: 
      print('The computer chooses scissors.') 
     #display player choice 
     if player_choice == "1": 
      print('You choose rock.') 
     elif player_choice == "2": 
      print('You choose paper.') 
     else: 
      print('You choose scissors.') 

     result = determine_winner(player_choice, computer_choice, choice2) 
     if result == 'computer': 
      number_of_computer_games += 1 
     elif result == 'player': 
      number_of_player_games += 1 
     else: 
      number_of_tied_games += 1 
     print  


main() 

回答

2

該問題與determine_winner方法有關。 方法內部的代碼將運行,而choice2的值不是4,但在循環過程中不會更改它,因此它不會停止循環。

有幾種解決這個問題,最簡單的就是根本不使用循環(畢竟,你應該檢查的結果只有每場比賽出場)。

def determine_winner(player_choice, computer_choice, choice2): 
    if computer_choice == 1: 
     if player_choice == "2": 
      print('Paper covers rock. You win!') 
      winner = 'player' 
     elif player_choice == "3": 
      print("Rock crushes scissors. The computer wins!") 
      winner = 'computer' 
     else: 
      print('The game is tied. Try again.') 
      winner = 'tied' 
    if computer_choice == 2: 
     if player_choice == "1": 
      print('Paper covers rock. The computer wins!') 
      winner = 'computer' 
     elif player_choice == "3": 
      print("Scissors cuts paper. You win!") 
      winner = 'player' 
     else: 
      print('The game is tied. Try again.') 
      winner = 'tied' 
    if computer_choice == 3: 
     if player_choice == "1": 
      print('Rock smashes scissors. You win!') 
      winner = 'player' 
     elif player_choice == "2": 
      print("Scissors cuts paper. The computer wins!") 
      winner = 'computer' 
     else: 
      print('The game is tied. Try again.') 
      winner = 'tied' 
    return winner 

這就是說,有您應該修正其他錯誤:

  • 循環位置是錯誤的。每次比賽結束時,您應顯示菜單並獲取數據。 (@ line75)
  • 您已經添加了選擇2變量,但你從來沒有改變它。我相信它應該是player_choice。 (@lines 62〜74)
  • 通過消除choice2變量,你不需要它傳遞給determine_winner方法。
  • 請記住,「number_of _ * _ games」變量應在全局escope中聲明,稍後在全局聲明,否則稍後您將無法在main()方法上訪問它們。

完整的完整代碼:

import random 

tied_games = 0 
player_games = 0 
computer_games = 0 

def main(): 
    display_menu() 
    print('There were', tied_games, 'tie games played.') 
    print('The computer won', computer_games, 'game(s).') 
    print('You won', player_games, 'game(s).') 


def process_computer_choice(): 
    choice1 = random.randint(1,3) 
    return choice1 


def process_player_choice(): 
    print('What is your choice?') 
    choice2 = (input()) 
    while choice2 != "1" and choice2 != "2" and choice2 != "3" and choice2 != "4": 
     print("ERROR: the choice can only be 1, 2, 3, or 4.") 
     choice2 = (input("Please enter a correct choice: ")) 
    return choice2 


def determine_winner(player_choice, computer_choice): 
    if computer_choice == 1: 
     if player_choice == "2": 
      print('Paper covers rock. You win!') 
      winner = 'player' 
     elif player_choice == "3": 
      print("Rock crushes scissors. The computer wins!") 
      winner = 'computer' 
     else: 
      print('The game is tied. Try again.') 
      winner = 'tied' 
    if computer_choice == 2: 
     if player_choice == "1": 
      print('Paper covers rock. The computer wins!') 
      winner = 'computer' 
     elif player_choice == "3": 
      print("Scissors cuts paper. You win!") 
      winner = 'player' 
     else: 
      print('The game is tied. Try again.') 
      winner = 'tied' 
    if computer_choice == 3: 
     if player_choice == "1": 
      print('Rock smashes scissors. You win!') 
      winner = 'player' 
     elif player_choice == "2": 
      print("Scissors cuts paper. The computer wins!") 
      winner = 'computer' 
     else: 
      print('The game is tied. Try again.') 
      winner = 'tied' 
    return winner 

def display_menu(): 
    global tied_games 
    global player_games 
    global computer_games 

    player_choice = 0 

    while player_choice != "4": 
     print('  MENU') 
     print('1) Rock') 
     print('2) Paper') 
     print('3) Scissors') 
     print('4) Quit') 
     print("Let's play the game of Rock, Paper, Scissors.") 
     computer_choice = process_computer_choice() 
     player_choice = process_player_choice() 

     if player_choice != "4": 
      if computer_choice == 1: 
       print('The computer chooses rock.') 
      elif computer_choice == 2: 
       print('The computer chooses paper.') 
      else: 
       print('The computer chooses scissors.') 

      #display player choice 
      if player_choice == "1": 
       print('You choose rock.') 
      elif player_choice == "2": 
       print('You choose paper.') 
      else: 
       print('You choose scissors.') 

      result = determine_winner(player_choice, computer_choice) 
      if result == 'computer': 
       computer_games += 1 
      elif result == 'player': 
       player_games += 1 
      else: 
       tied_games += 1 


if __name__ == '__main__': 
    main() 

我相信這是所有:)

+1

非常感謝你。保存我的屁股。我沒有完全像你這樣做,但這幫助我瞭解到底發生了什麼以及我出錯的地方。程序功能現在完美。再次感謝。 – Alex

+0

該代碼純粹是爲了演示我已經注意到的完整「更正」,而不是完全被複制/粘貼。 –

+1

我沒有複製/粘貼它.....我只是不斷創建無限循環,看到你是如何做到這一點,使我重新思考我的方法。 – Alex

5

你的程序將無限運行,因爲你沒有更新的變量choice2display_menu()功能。你在做什麼:

choice2 = 0 
while choice2 != "4": 
    # your code 
    result = determine_winner(player_choice, computer_choice, choice2) 

所以,最終choice2總是0,你的while循環不斷運行。

編輯:您從process_player_choice()返回值和內部display_menu()函數的值賦給變量player_choice

player_choice = process_player_choice() 

我想你應該返回值賦給變量choice2,這樣當用戶給出4作爲輸入,程序將被終止。

choice2 = process_player_choice() 

注意,你有內部determine_winner()另一個while循環將運行不休,以及因爲choice2。我相信while循環不是必需的,因爲你已經在display_menu()函數中有一個while循環。

因此,簡而言之,您實際上不需要額外的變量player_choice,您只需要一個變量來存儲用戶選擇並可以傳遞到所需的函數來執行其操作。您還應該省略display_menu()函數中的while循環。

最後,正如@AdrianoMartins在他的回答中提到的那樣,您在display_menu()中聲明的以下變量應該是全局的,否則您將無法從main()函數訪問它們。

number_of_tied_games = 0 
number_of_player_games = 0 
number_of_computer_games = 0 

可以在全球範圍內宣佈他們,然後把他們定義爲全球內display_menu()修改自己的價值。例如:

// declaring globally 
tied_games = 0 
player_games = 0 
computer_games = 0 

def display_menu(): 
    // need to modify global copy of the variables 
    global tied_games 
    global player_games 
    global computer_games 

要了解更多信息,我鼓勵您看到此SO post

+0

我應該怎麼定義選擇2作爲display_menu ...?它說當我嘗試運行,但選擇2應該由process_player_choice – Alex

+1

定義@Alex我已經更新了我的答案選擇2沒有定義。請檢查。 –

+0

謝謝先生,您的幫助和時間。 – Alex