2014-10-19 86 views
0

我正在編寫一個編程作業,並且遇到了一個小小的障礙,我正在爲一個岩石,紙張,剪刀遊戲工作。該程序假設由用戶選擇4個選項中的1個,1)搖滾,2)紙,3)剪刀和4)退出。一旦玩家選擇了一個選項,計算機的選擇就會顯示出來,獲勝者將被宣佈,程序會詢問你是否想玩另一個遊戲。如果選擇y,則返回主菜單選擇另一個選項,其他任何選項都會顯示贏得的比賽數量,丟失的比賽以及比賽結束時的比賽數量。如果玩家選擇4,則該程序應該說「退出程序...」,並且應該顯示遊戲結果。Python 3岩石,紙張,剪刀問題

這裏是我的問題:

  1. 一旦你做出了第一個選擇,顯示贏家和程序返回到主菜單。如果您再次選擇,它會通知您電腦選擇的內容,然後詢問您是否想再次播放。 Y會帶你回到主菜單,電腦的選擇永遠不會改變,無論你選擇什麼遊戲,總是會以與第一場比賽相同的結果結束。如果您選擇不再玩,那麼會出現贏,輸和遊戲的數量(這似乎是正常運行)。

  2. 退出選項會將您帶回主菜單,而不是顯示遊戲結果。我不知道該在哪裏發表聲明。

任何有關這些問題的幫助,將不勝感激。

謝謝

#import module 
import random 

def main(): 
    #create a variable to control the loop 
    play_again = 'y' 

    #create a counter for tied games, computer games and player games 
    tied_games = 0 
    computer_games = 0 
    player_games = 0 

    #display opening message 
    print("Let's play rock, paper scissors!") 

    computer_choice = process_computer_choice() 

    player_choice = process_player_choice() 

    winner = determine_winner(player_choice, computer_choice) 

    #setup while loop for playing multiple games 
    while play_again == 'y' or play_again == 'Y': 

     process_computer_choice() 

     process_player_choice() 

     #use a if else statement to print the computers choice 
     if computer_choice == 1: 
      print('computer chooses rock.') 

     elif computer_choice == 2: 
      print('computer chooses paper.') 

     else: 
      print('computer chooses scissors.') 

      #call the determine winner function  
      determine_winner(player_choice, computer_choice) 

     #check who won the game and add 1 to the correct counter 
     if winner == 'computer': 
      computer_games += 1 

     elif winner == 'player': 
      player_games += 1 

     else: 
      tied_games += 1 

     #ask the user if they would like to play again  
     play_again = input('would you like to play again? (enter y for yes): ') 

    #display number of games that were won by the computer, the player and that were tied 
    print() 
    print('there was', tied_games, 'tied games.') 
    print('the player won', player_games, 'games.') 
    print('The computer won', computer_games,'games.') 

#define the process computer function 
def process_computer_choice(): 

    #setup computer to select random integer between 1 and 3 
    choice1 = random.randint(1, 3) 

    #return the computers choice 
    return choice1 

#define the process player function 
def process_player_choice(): 

    #add input for players choice 
    print() 
    print('  MENU') 
    print('1) Rock!') 
    print('2) Paper!') 
    print('3) Scissors!') 
    print('4) Quit') 
    print() 

    player_choice = int(input('Please make a selection: ')) 

    #add if statement for quit option 
    if player_choice == 4: 
     print('Exiting program....') 

    #validate if the user enters a correct selection 
    while player_choice != 1 and player_choice != 2 and player_choice != 3 and player_choice != 4: 

     #print a error message if the wrong selection is entered 
     print('Error! Please enter a correct selection.') 

     player_choice = int(input('Please make a selection: ')) 

    #return the players choice 
    return player_choice 

#define the determine winner function 
def determine_winner(player_choice, computer_choice): 

    #setup if else statements for each of the 3 computer selections 
    if computer_choice == 1: 
     if player_choice == 2: 
      print('Paper wraps rock. You win!') 
      winner = 'player' 

     elif player_choice == 3: 
      print('Rock smashes 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 wraps rock. The computer wins!') 
      winner = 'computer' 

     elif player_choice == 3: 
      print('Scissors cut 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 cut paper. The computer wins!') 
      winner = 'computer' 

     else: 
      print('The game is tied. Try again.') 
      winner = 'tied' 

    return winner 

main() 

回答

2

對於問題1,這是因爲您在循環之前設置的計算機和球員的選擇,從不更新它們。你的循環的開始更改爲:

while play_again == 'y' or play_again == 'Y': 
    computer_choice = process_computer_choice() 
    player_choice = process_player_choice() 

另外,還可以檢查輸入的贏家,因爲它是第一輪技術上冗餘環路之前刪除的代碼行。

對於問題2,只需添加結果選擇了4後,像這樣:

if player_choice == 4: 
     print('Exiting program....') 
     print('there was', tied_games, 'tied games.') 
     print('the player won', player_games, 'games.') 
     print('The computer won', computer_games,'games.') 
     sys.exit() # be sure you add 'import sys' to the beginning of your file 

此外,線路在主迴路determine_winner(player_choice, computer_choice)縮進所以它纔會被調用,如果計算機選擇剪刀,所以你應該unindent::)

+0

只是使用返回而不是sys.exit – 2014-10-19 23:14:13

+0

他檢查玩家的選擇是否是在由main調用的函數中是4,所以不需要退出?顯然,最好的辦法是檢查main(),但是用他的結構不是這樣嗎? – Parker 2014-10-19 23:23:56

+0

這正是我所需要的。謝謝! – 2014-10-20 01:12:45

0

您還沒有分配給computer_choiceplayer_choice一遍,但使用它的價值。

while play_again == 'y' or play_again == 'Y': 

    process_computer_choice() 

    process_player_choice() 

應該

while play_again == 'y' or play_again == 'Y': 

    computer_choice = process_computer_choice() 

    player_choice = process_player_choice() 

至於退出只是在退出選擇突破。您必須儘早從process_player_choice返回,並在main中執行一些操作。

所以在process_player_choice:

if player_choice == 4: 
    print('Exiting program....') 
    return 

,並在主: player_choice = process_player_choice()

if player_choice == 4: 
    return 

winner = determine_winner(player_choice, computer_choice) 

#setup while loop for playing multiple games 
while play_again == 'y' or play_again == 'Y': 

    computer_choice = process_computer_choice() 

    player_choice = process_player_choice() 
    if player_choice == 4: 
     break 
+0

'如果player_choice == 4:'不在循環中,所以你不能使用break,你會使用return – 2014-10-19 23:17:11

+0

@PadraicCunningham,'while play_again =='y'或者play_again == 'Y':'不是一個循環? – 2014-10-19 23:27:39

+0

我的解決方案意味着在第一輪退出時什麼都不會做,但之後會正常工作。這可能不是理想的。 – 2014-10-19 23:31:27