2012-10-29 47 views
2

我正在一塊岩石紙剪刀遊戲。一切似乎都運作良好,除了贏/輸/領帶臺。我看過一些人在這裏發佈的其他遊戲,但仍然無法讓我的工作。我覺得我很親近,但我無法得到它!感謝任何幫助傢伙。這是我第一次在這裏發佈,所以我很抱歉,如果我搞砸了格式。Python岩石剪刀計分器

我編輯了代碼,但仍然無法讓程序在不使用全局變量的情況下識別計數器。在我編輯的某個時刻,我設法讓它把所有東西都算作一條平行線......我不知道如何,我在編輯過程中丟失了它。大聲笑。再次感謝大家!

這裏是我所得到的,當我運行程序:

Prepare to battle in a game of paper, rock, scissors! 
Please input the correct number according 
to the object you want to choose. 
Select rock(1), paper(2), or scissors(3): 1 
Computer chose PAPER . 
You chose ROCK . 
You lose! 
Play again? Enter 'y' for yes or 'n' for no. y 
Prepare to battle in a game of paper, rock, scissors! 
Please input the correct number according 
to the object you want to choose. 
Select rock(1), paper(2), or scissors(3): 2 
Computer chose PAPER . 
You chose PAPER . 
It's a tie! 
Play again? Enter 'y' for yes or 'n' for no. y 
Prepare to battle in a game of paper, rock, scissors! 
Please input the correct number according 
to the object you want to choose. 
Select rock(1), paper(2), or scissors(3): 3 
Computer chose SCISSORS . 
You chose SCISSORS . 
It's a tie! 
Play again? Enter 'y' for yes or 'n' for no. n 
Your total wins are 0 . 
Your total losses are 0 . 
Your total ties are 0 . 

#import the library function "random" so that you can use it for computer 
#choice 
import random 

#define main 
def main(): 
    #assign win, lose, and tie to zero for tallying 
    win = 0 
    lose = 0 
    tie = 0 

    #control loop with 'y' variable 
    play_again = 'y' 

    #start the game 
    while play_again == 'y': 
     #make a welcome message and give directions 
     print('Prepare to battle in a game of paper, rock, scissors!') 
     print('Please input the correct number according') 
     print('to the object you want to choose.') 

     #Get the player and computers choices and 
     #assign them to variables 
     computer_choice = get_computer_choice() 
     player_choice = get_player_choice() 

     #print choices 
     print('Computer chose', computer_choice, '.') 
     print('You chose', player_choice, '.') 

     #determine who won 
     winner_result(computer_choice, player_choice) 

     #ask the user if they want to play again 
     play_again = input("Play again? Enter 'y' for yes or 'n' for no. ") 

    #print results 
    print('Your total wins are', win, '.') 
    print('Your total losses are', lose, '.') 
    print('Your total ties are', tie, '.') 

#define computer choice 
def get_computer_choice(): 
    #use imported random function from library 
    choice = random.randint(1,3) 

    #assign what the computer chose to rock, paper, or scissors 
    if choice == 1: 
     choice = 'ROCK' 
    elif choice == 2: 
     choice = 'PAPER' 
    else: 
     choice = 'SCISSORS' 

    #return value 
    return choice 

#define player choice 
def get_player_choice(): 
    #assign input to variable by prompting user 
    choice = int(input("Select rock(1), paper(2), or scissors(3): ")) 

    #Detect invalid entry 
    while choice != 1 and choice != 2 and choice != 3: 
     print('The valid numbers are rock(type in 1), paper(type in 2),') 
     print('or scissors(type in 3).') 
     choice = int(input('Enter a valid number please: ')) 

    #assign what the player chose based on entry 
    if choice == 1: 
     choice = 'ROCK' 
    elif choice == 2: 
     choice = 'PAPER' 
    else: 
     choice = 'SCISSORS' 

    #return value 
    return choice 

#determine the winner from the variables 
def winner_result(computer_choice, player_choice): 
    #if its a tie, add 1 to tie variable and display message 
    if computer_choice == player_choice: 
     result = 'tie' 
     print("It's a tie!") 

    #if its a win, add to win tally and display message 
    elif computer_choice == 'SCISSORS' and player_choice == 'ROCK': 
     result = 'win' 
     print('ROCK crushes SCISSORS! You win!') 
    elif computer_choice == 'PAPER' and player_choice == 'SCISSORS': 
     result = 'win' 
     print('SCISSORS cut PAPER! You win!') 
    elif computer_choice == 'ROCK' and player_choice == 'PAPER': 
     result = 'win' 
     print('PAPER covers ROCK! You win!') 

    #if it does not match any of the win criteria then add 1 to lose and 
    #display lose message 
    else: 
     result = 'lose' 
     print('You lose!') 

def result(winner_result,player_choice, computer_choice): 

    # accumulate the appropriate winner of game total 
    if result == 'win': 
     win += 1 
    elif result == 'lose': 
     lose += 1 
    else: 
     tie += 1 
    return result 

main() 
+2

coursera作業:) –

+3

它看起來像你有一些麻煩粘貼你的代碼與縮進完好無損。解決這個問題非常重要,因爲您的問題可能歸結爲流量控制,而縮進常常影響Python中的流量控制。也可以更清楚地說明當前程序出了什麼問題 - 您爲測試投入了什麼輸入,您期望的輸出以及您獲得的輸出。 –

+0

縮進總是影響Python中的流量控制。 – Wug

回答

2

winner_result函數返回它增加了取勝的計數器。如果您從中刪除所有return語句,則應更新計數器。 return聲明並不需要,因爲if/elif/else結構確保只有一個可能的結果將被執行。

正如Junuxx在評論中所述,您還需要正確指定winner_result變量的值,即winner_result = 'win'而不是winner_result == 'win'。我還會重命名winner_result變量或函數,因爲它們都使用相同的名稱會令人困惑。

win/lose/tie變量當前是本地的,這意味着mainwinner_result將具有它們自己的這些變量的副本,因此main的值將始終爲零。你可以做的是使它們成爲全局變量:在全局範圍內(在任何函數之外)將它們分配爲零,並在函數winner_result內添加global win, lose, tie行。

+0

'winner_result'也有一堆線條,比如'winner_result =='win'',它們沒有任何影響;他們應該有一個'='分配。然後是「贏/輸/平局」的範圍。 – Junuxx

+0

我想我最終修復了格式。我會通過取消退貨來嘗試。謝謝! –

+0

@Junuxx好的發現,我在回答中加入了這個。 – interjay