Home > other >  How to make a function to score a Rock, Paper and Scissor game in Python?
How to make a function to score a Rock, Paper and Scissor game in Python?

Time:04-28

I wrote a Rock, Paper and Scissor game in Python. And I want to have a function that scores the game. The only way I managed to do that was by using global variables inside the function. I know that's not a good practice and I'd like to know how can I make this function without the need for global variables.

import random

def validate_user_input(input):
    """Verify if user input is a valid."""
    try:
        usr_input = int(input)
    except:
        return 0
    else: 
        if (usr_input >= 1) and (usr_input <= 3):
            return usr_input
        else:
            return 0

def compare_results(player_choice):
    """Get computer choice and compare it with user choice"""
    computer_option = random.randint(1,3)    
    result_to_word = {0:'- Draw', 1:'- You loose.', 2:'- You Win'}
    if player_choice == computer_option:
        result = 0
    if (player_choice == 1 and computer_option == 2):
        result = 1
    if (player_choice == 1 and computer_option == 3):
        result = 2
    if (player_choice == 2 and computer_option == 1):
        result = 2
    if (player_choice == 2 and computer_option == 3):
        result = 1
    if (player_choice == 3 and computer_option == 1):
        result = 1
    if (player_choice == 3 and computer_option == 2):
        result = 2  
    
    return (result, result_to_word[result], computer_option)

def codify_result(input):
    "Transform number of choice into word"
    num_to_word = {1: "Rock", 2: "Paper", 3:"Scissor"}
    return num_to_word[input]

def make_score(result):
    global computer_score
    global player_score
    if result == 1:
        computer_score  = 1                
    elif result == 2:
        player_score  = 1


player_score = 0
computer_score = 0

intro = "\nHello!\nLet's play 'Rock, Paper and Scissors'.\nChoose an option and wait to see the result (Press 'q' at any time to exit)"
print(intro)

while True:
    user_input = input("\n1) Rock, 2) Paper os 3) Scissors?: ")
    if (user_input == 'q'):
        break
    else:        
        user_choice = validate_user_input(user_input)
        if user_choice == 0:
            print("You have to choose a number between 1 and 3.")
        else:
            result = compare_results(user_choice)           
            print("You chose: "   codify_result(user_choice)   ".")
            print("The computer chose: "   codify_result(result[2])   '.')
            print(result[1])
            make_score(result[0])
            print("You: "   str(player_score)   "\nComputer: "   str(computer_score))

So how could I implement this function in a better way?

CodePudding user response:

A "pure functional" approach could be to change your make_score function to take the old scores as arguments, and then return the updated scores:

def make_score(result, computer_score, player_score):
    if result == 1:
        computer_score  = 1                
    elif result == 2:
        player_score  = 1
    return computer_score, player_score

When you call make_score you pass the old scores and assign the new scores:

computer_score, player_score = make_score(result[0], computer_score, player_score)

Taking a step back, though, I might suggest taking an approach that doesn't require you to have a function that translates an opaque result int value into a modification to one or another variable. Maybe put the scores in a dict:

scores = {
    "computer": 0,
    "player": 0,
}

and then instead of a magic result int, assign a winner that matches one of the dict keys:

    if (player_choice == 1 and computer_option == 2):
        winner = "computer"

and then you can get rid of the make_score function completely and just do:

scores[winner]  = 1

CodePudding user response:

I would recommend making a dictionary which you can then pass into make_score

score_dict = {'computer_score': 0, 'player_score': 0}
make_score(score_dict, result):
    if result == 1:
        score_dict['computer_score']  = 1    
    ...
  • Related