2012-10-29 5 views
2

나는 바위 종이 가위 게임을하고있다. 승패/동점 카운터를 제외한 모든 것이 잘 작동하는 것 같습니다. 사람들이 여기에 게시 한 다른 게임 중 일부를 살펴 보았지만 여전히 광산을 사용할 수 없습니다. 나는 내가 가까운 soooooo 인 것처럼 느낀다. 그러나 나는 단지 그것을 얻을 수 없다! 어떤 도움을 주셔서 감사합니다. 이것은 처음으로 여기에 게시되므로 서식을 엉망으로 만들면 미안합니다.파이썬 바위 종이 가위 스코어 카운터

코드를 편집했지만 전역 변수를 사용하지 않고 카운터를 인식하는 프로그램을 가져올 수 없습니다. 편집의 한 시점에서 나는 모든 것을 넥타이로 계산할 수 있었다. 나는 어떻게 편집해야할지 모르겠다. 롤. - 모두 다시 한 번 감사합니다! 이 승리 카운터를 증가

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

들여 쓰기가있는 코드를 붙여 넣는 데 문제가있는 것처럼 보입니다. 여러분의 문제는 유동 제어로 이어질 수 있고 들여 쓰기는 종종 파이썬의 흐름 제어에 영향을 미치기 때문에이 문제를 해결하는 것이 중요합니다. 또한 현재 프로그램에 무엇이 잘못되었는지에 대해 더 명확히 밝히는 것이 좋습니다. 테스트 용으로 입력 한 내용, 예상되는 출력 및 얻을 수있는 결과물입니다. –

+0

들여 쓰기는 항상 파이썬의 흐름 제어에 영향을 미칩니다. – Wug

답변

2

귀하의 winner_result 함수가 반환하기 전에 : 여기

내가 프로그램을 실행할 때 내가 무엇을 얻을 수 있습니다. 모든 return 문을 제거하면 카운터가 업데이트되어야합니다. if/elif/else 구조는 가능한 결과 중 하나만 실행되도록하기 때문에 return 문은 필요하지 않습니다.

Junuxx가 의견에서 말한 것처럼 winner_result 변수에 적절하게 값을 할당해야합니다 (예 : winner_result == 'win' 대신 winner_result = 'win'). 또한 변수 또는 함수의 이름을 바꾸는 것이 좋습니다. 동일한 이름을 사용하는 것이 혼란하기 때문입니다.

그리고 win/lose/tie 변수는 mainwinner_result 그렇게 main의 값은 항상 0이됩니다, 이러한 변수의 자신의 사본을해야합니다 즉, 현재 지역이다. 당신이 할 수있는 일은 전역 변수를 만드는 것입니다 : 전역 범위 (함수 밖에서)를 0에 할당하고, winner_result 함수 안에 global win, lose, tie 줄을 추가하십시오.

+0

'winner_result'도 효과가없는'winner_result == 'win''과 같은 줄을 가지고 있습니다; 그들은 할당을 위해 하나의'= '를 가져야한다. 그리고 '승/패/동점'의 범위가 있습니다. – Junuxx

+0

필자는 마지막으로 서식을 수정했다고 생각합니다. 나는 그 보상을 제거함으로써 시도 할 것이다. 감사! –

+0

@Junuxx 좋은 발견, 나는 대답에 이것을 추가했다. – interjay

관련 문제