1

I'm trying to make a very simple and kinda archaic Rock-paper-scissors game, with only to player and to be run through the console.

I have something like this:

def rps():
    player_hand = input('>_')

    machine_hand_opt = ['rock', 'paper', 'scissors']
    machine_hand = random.choice(machine_hand_opt)

    who_win = machine
    if machine_hand == 'rock' and player_hand == 'scissors':
        who_win
        return who_win
    elif clown_hand == 'rock' and player_hand == 'paper':
        who_win = 'user'
        return who_win

This is only part of the code, and you can imagine the remaining. As you can see, the machine hand is chosen randomly from a list, I use the variable 'who_win' to get who wins and then print something, but when the result is printed, the user only knows it's own hand, and not machine's hand, because the functions only returns who wins, is obviously simple to figure out machine's hand if player wins or loose, but I think this is a good chance to know how to print variables outside functions. It would be something like:

print("Player's hand:", player_hand)
print("Machine's hand:", machine_hand)

Any ideas?

4 Answers 4

4

You can return multiple values from a function; just use a tuple:

return machine_hand, player_hand, who_win

The caller of the function can then unpack the tuple again into three variables:

machine_hand, player_hand, who_won = rps()

The names of those three variables are local to the caller, and names do not have to match; I used who_won when unpacking, for example.

You may want to think about how much work rps() does here; personally, I'd limit the function to determining who won alone. Ask the user for input in a separate function (you can do validation there too), determine the computer choice in another, and have this function only return the winner:

def game():
    while True:
        player_hand = ask_player_for_choice()
        machine_hand = determine_machine_choice()
        who_won = determine_winner(player_hand, machine_hand)
        display_winner(player_hand, machine_hand, who_won)
        if not ask_to_play_again():
            return

Note that now all you have to return is the winner from a determine_winner() function. The whole game was put in an endless loop, that ends when the player doesn't want to play again (ask_to_play_again() returns False).

And last but not least, a tip: determining the winner can be as easy as a dictionary lookup. Map out, in a dictionary, what hand beats what. If the hands are not equal, both lose, but otherwise if beats_map[machine_hand] == player_hand you know exactly who won.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for you tips, I'll keep it in mind, but the idea here is to keep it simple. But now I have more knowledge for when I need it. I use a variable called result = rps() to use rps' result, and that's how I determine who won, but I don't know how could it be if I return these three functions. How could I get the result then?
@AndresOrozco: you mean 3 values? You would use machine_hand, player_hand, result = rps(), where result would still be the same value. Or you can use result = rps(), where result is now a tuple and result[0] is the machine hand, result[1] the player hand, and result[2] who won the game.
2

You can either return both hands:
(inside function)

return who_won, machine_hand

(outside function)

print(*rps())

or use a global variable (Keyword is "global" - but be warned, global is very powerful!)

Comments

1

You have several options:

  1. Return machine_hand as well, like that:

return who_win, machine_hand

Outside of yoir method, you can get the results in this way:

who_win, machine_hand = rps()

  1. Define machine_hand outside of your method and use it as a global (which might be an "easier" option, however in general it's better to avoid globals if possible, because their presence encourages messy code):

`

machine_hand = None

def rps():
    global machine_hand
   # your code.. 

`

One other remark: it's often preferable to keep separate days input and data processing, so you might consider reading human_hand in your outside loop and giving it to your rps() method as an argument.

Comments

0

To make it simple: Why don'y you just print the result inside the function? Because it sounds like a job belongs to rps.

If you insist or have future usage for machine_hand, just return multiple value like @Martijin mentioned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.