Hi
I’m a newbie in python.
I’m trying to create my own rock, paper, scissors game, but kind of stuck now.
when running the code, “player_choice” does not come out right.
Any help will be appreciated.
import random
s = "Scissors"
r = "Rock"
p = "Papper"
options = [r, p, s]
player_choice = input("Choose r = Rock, p = Papper, s = Scissors\n").lower()
print("You:", player_choice)
computer_choice = random.choice (options)
print("Computer:",computer_choice)
import random
choices = {
"s": "Scissors",
"r": "Rock",
"p": "Paper",
}
# the keys for choices are s, r, and p
options = list(choices.keys())
player_choice = input("Choose r = Rock, p = Paper, s = Scissors\n").lower()
# Notice I'm using .get with a default value of Unknown in case the user gives something other than r, p, or s
print("You:", choices.get(player_choice, "Unknown"))
# Randomly pick one of s, r, or p
computer_choice = random.choice (options)
# convert s, r, or p to the full word.
print("Computer:",choices[computer_choice])
Then running:
python /tmp/t.py
Choose r = Rock, p = Paper, s = Scissors
p
You: Paper
Computer: Rock
Hi Charles
I’ve completed my rock paper scissors game code, one problem I encountered was that if I input an unknow character, it won’t print ’ wrong input’ I asked it to .
Could you please have a look. Thanks
import random
choices = {"r": "Rock", "p": "Paper", "s": "Scissors"}
# the keys for choices are s, r, and p
options = list(choices.keys())
player_choice = input("Choose r = Rock, p = Paper, s = Scissors\n").lower()
# Notice I'm using .get with a default value of Unknown in case the user gives something other than r, p, or s
print("You:",choices.get(player_choice, "Unknown"))
# Randomly pick one of s, r, or p
computer_choice = random.choice (options)
# convert s, r, or p to the full word.
print("Computer:",choices[computer_choice])
if(player_choice == "Unknown"):
print("Wrong input, try again")
elif(player_choice == computer_choice):
print("It's a draw")
elif(player_choice == "r" and computer_choice == "s") or \
(player_choice == "p" and computer_choice == "r") or \
(player_choice == "s" and computer_choice == "p"):
print("You win!")
else:
print("You lose!")
When I run it it prints Unknown if I enter an unrecognized choice:
>>> choices = {"r": "Rock", "p": "Paper", "s": "Scissors"}
>>>
>>> # the keys for choices are s, r, and p
>>> options = list(choices.keys())
>>>
>>> player_choice = input("Choose r = Rock, p = Paper, s = Scissors\n").lower()
Choose r = Rock, p = Paper, s = Scissors
d
>>> print("You:",choices.get(player_choice, "Unknown"))
You: Unknown
If what you want to happen is that it loops until the player selects a valid choice try replacing the input line with this:
while (player_choice := input("Choose r = Rock, p = Paper, s = Scissors\n").lower()) not in choices:
print(f'Invalid option {player_choice} selected.')
There’s a lot in that so I’m going to break it down for you. while is a loop that runs as long as a condition is met. player choice := ... is how you assign to a variable in a conditional statement (the := is commonly called the “walrus operator”.) not in choices checks to see if the option the player suggested is one of the keys in the choices dictionary. f'...' is called an f-string. It’s a formatted string. We can include the variable values by enclosing them in brackets ({}) so if the player selected d it would print “Invalid option d selected.”
The code snippet I gave will ask a player for their choice. If the choice is valid (is one of the keys in the choices dictionary) it will continue with the logic and output the results of the game. If not it will print that an invalid option was selected and ask for a new selection.
Another approach you could take is to abort the game if an invalid choice is selected. You could accomplish this by replacing the while with if and adding an exit(1) after the print statement. (The reason I use exit(1) and not exit() or exit(0) is that a non-zero exit value tells the OS that an error occurred.)
Thanks Dan for your code.
The while/loop is a much better choice for this game. And your explanations are immensely helpful.
I do want the game to repeat after each results, please advise how I go about this.
Many thanks!
You can put the whole thing in a while True loop. If you do I would ask at the end of each game if the player wants to play again and if they say “No” (or perhaps if they do not say “Yes”) break from that while loop.