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.