Comprehension problem function with arguments

Hi!

This is the original code from the Python for beginners course, some of which I don’t understand.
I have modified the code a little so that I can understand it better. My modified code follows below.

import random


def get_choices():
  player_choice = input("Enter a choice: ")

  options = ["rock", "paper", "scissors"]
  computer_choice = random.choice(options)

  # dictionaries: key+value pairs, key OR value -->variable
  dingens = {"player": player_choice, "computer": computer_choice}
  return dingens


def check_win(player, computer):
  # Variable and string mix with "f-string" and curly braces
  print(f"You chose {player}, computer chose {computer}.")

  if player == computer:
    return "It's a tie!"
  elif player == "rock":
    if computer == "scissors":
      return "Rock smashes scissors. You win!"
    else:
      return "Paper covers rock. You lose."
  elif player == "paper":
    if computer == "rock":
      return "Paper covers rock! You win!"
    else:
      return "Scissors cuts paper! You lose."
  elif player == "scissors":
    if computer == "paper":
      return "Scissors cuts paper! You win!"
    else:
      return "Rock smashes scissors. You lose."
      
choices = get_choices()
result = check_win(choices["player"], choices["computer"])
print(result)
import random


def player_choice():
  p = input("Enter a choice: ")
  return p

def computer_choice():  
  options = ["rock", "paper", "scissors"]
  c = random.choice(options)
  return c

player=player_choice()
computer=computer_choice()

# print(player)
# print(computer)

def check_win():
  # Variable and string mix with "f-string" and curly braces
  print(f"You chose {player}, computer chose {computer}.")

  if player == computer:
    return "It's a tie!"
  elif player == "rock":
    if computer == "scissors":
      return "Rock smashes scissors. You win!"
    else:
      return "Paper covers rock. You lose."
  elif player == "paper":
    if computer == "rock":
      return "Paper covers rock! You win!"
    else:
      return "Scissors cuts paper! You lose."
  elif player == "scissors":
    if computer == "paper":
      return "Scissors cuts paper! You win!"
    else:
      return "Rock smashes scissors. You lose."


result = check_win()
print(result)

I don’t understand the connection between the check_win function with player and computer in the brackets and the “result” in the code from the course.
I don’t understand why in the check_win function, player and computer belong in the brackets.
In the simplified version of mine it is enough to just “execute” the check_win (without player and computer in the brackets)!?

I’ve watched it quite a few times, but I can’t understand Beau Carnes explanation there, he doesn’t say much at that point either.
I hope you understand my problem and someone can explain it to me in simple words.

Many thanks in advance.

There’s a very loose connection. check_win function expects two arguments - with the representation of each player choice - to determine who won. It doesn’t really care from where these values are coming from, all it knows it that it will receive two arguments. check_win returns then string that represents the result.

This one? print(f"You chose {player}, computer chose {computer}.")

Well, wasn’t that a point of simplifying it? Consider though, how easy is to play another round.

I meant here:

def check_win(player, computer):

Why do player and computer have to be in brackets here?
It doesn’t seem to be necessary for the function itself. But it is for the entire code, because it won’t work without it. That’s where my understanding gets stuck.

it’s quite necessary instead, as a function is a piece of reusable code, the function parameters are there to be inputted a value, and for different values the function will give the appropriate response

Function check_win is defined to have two parameters. They need to be passed to function when it’s called - for example: check_win(choices["player"], choices["computer"]). Otherwise, within the function every time there would be player or computer used, they would be taken from outside of the function scope - if there weren’t be defined outside of the function scope script would crash.

As said earlier - consider for both versions what would be needed to play another round.

1 Like

Thank you, I understand that now.

But not with the another game. I did a bit of googling and found this solution, where I put the whole game into a function and repeat it with:

  again = input("Do you want to play again (type yes or no): ")
  if again == "yes":
    game()
  else:
    sys.exit(0)

I assume you were getting at something else, but that’s beyond me. I think I still need to learn some basics. My experience with programming as a child was with Basic on MS-DOS 3.3. The first thing that came to my mind with your question was “goto 10” :smile:

Best regards

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.