Skip to content
← Back to Community
Need help with Rock, Paper, Scissors Python game ...
Profile icon
Airbus

Hello,

Can someone kindly help me with some code for a "Rock, Paper, Scissors" game in Python, please??

I can't find any good line-by-line explanations online on how to do it. All the solution entries that experts have typed list computers as players - but I don't want to include a computer player (i.e. with randint/random - whatever it is in my code), but the input of two (human) players.

I need to understand what code goes where & why.

I have attempted numerous times with the coding but have hit solid headwinds every time - because I don't know how to finish it : (

I also need to add a few more lines of code to what I've composed below for the game to work, but this is what I've done so far ...

Thanking you in advance for your help ... a BIG thank you!

-A.


print("Welcome to Rock, Paper, Scissors!")
print("Let's Begin ...")
name1 = input("Player 1: What's your name?")
name2 = input("Player 2: What's your name? \n")

print("Hello, " + name1 + " and " + name2)
print(name2 + ": Close your eyes!")
choice1 = input(name1 + ": enter 'r' for rock, 'p' for paper, and 's' for scissors: ")
print("Great choice! Now - cover your answer and ask " + name2 + " to chose. \n\n\n" )

if (name1 == name2):
print("Both are equal, so try again!")

elif(name1 == 'r' and name2 == 'p'):
print(name2 + ": won!")
print("Lets play again!")
elif(name1 == 'p' and name2 == 'r'):
print(name1 + ": won!")
print("Lets play again!")
elif(name1 == 's' and name2 == 'r'):
print(name2 + ": won!")
elif(name1 == 'r' and name2 == 's'):
print(name1 + ": won!")
elif(name1 == 'p' and name2 == 's'):
print(name2 + ": won!")
elif(name1 == 's' and name2 == 'p'):
print(name1 + ": won!")
elif(name1 == 'r' and name2 == 's'):
print(name1 + ": won!")
elif(name1 == 's' and name2 == 'r'):
print(name2 + ": won!")
else:
print("Thanks for playing Rock, Paper, Scissors!")

Voters
Profile icon
claudiaazz
Profile icon
CarsonNH
Profile icon
Airbus
Comments
hotnewtop
Profile icon
[deleted]

So you want me to explain how your code works?

Profile icon
Airbus

@JacksonCowie Hi Jackson,
I'd really appreciate it if you could, please.
Cheers,
A.

Profile icon
[deleted]

@Airbus Okay, I mean you should know what it does if you wrote it.

The first 4 lines of code introduces the user to the program and gets the first 2 user inputs (Their names)
The next 4 lines of code get player 1's input and stores it in the variable "choice1"

NOTE: This program isn't actually complete - you haven't gotten input from the second user. So just copy/paste those previous 4 lines but change the vars and text accordingly.

The next 2 lines of code check if the names of the users are the same, if they are then the execution stops after presenting a message.

The next chunk of code checks who would win by comparing all possible outcomes through an if statement and displays who would have won.

NOTE: You also need to be comparing the "choice1" variables - not the names entered.

Profile icon
[deleted]

So yeah - make sure the program actually works before tryna figure out what it does

Profile icon
Airbus

@JacksonCowie Hi Jackson, thanks for your help, I really appreciate it : ) Am very new to Python, so make a few mistakes here & there. I understood quite a bit of the code but got thrown out by some of the syntax/structure of it; esp. the sections around:

print("Hello, " + name1 + " and " + name2)
print(name2 + ": Close your eyes!")
choice1 = input(name1 + ": enter 'r' for rock, 'p' for paper, and 's' for scissors: ")
print("Great choice! Now - cover your answer and ask " + name2 + " to chose. \n\n\n" )

if (name1 == name2):
print("Both are equal, so try again!")

... That part - and how to compose the correct code for it - still throws me out a bit.

Am I able to ask you some more Python questions in the (near) future, please??

Thanks again.

  • A.
Profile icon
[deleted]

@Airbus np, so basically for the first bit you are tryna get the user input. Here is how I would do it:

print("Hello, " + name1 + " and " + name2) print(name2 + ": Close your eyes!") choice1 = input(name1 + ": enter 'r' for rock, 'p' for paper, and 's' for scissors: ") print("Great choice! Now - cover your answer and ask " + name2 + " to chose. \n\n\n" ) choice2 = input(name2 + ": enter 'r' for rock, 'p' for paper, and 's' for scissors: ") # Getting second user input if (choice1 == choice2): # Notice here how we are comparing the 2 inputs print("Both are equal, so try again!")

For the rest of the code it should work, providing that you change name1 to choice1 and name2 to choice2

Oh and you also forgot to compare with scissors.

Profile icon
Airbus

@JacksonCowie Hi Jackson, thanks for your help : )

I tried running the code with the changes you suggested & I couldn't get it to run - Argh!!

It said something about not defining name1 and name2 but I changed them.

Basically, I need to finish the code so that it works. But I also need to add the if/elif function to it too. I think there are nine possible outcomes with the Rock, Paper, Scissors game but am finding it very hard to conceptualise and think in Python code to compose them; i.e. I'm struggling with how to translate - from English into Python code - the steps in the Rock, Paper, Scissors game that I need to outline. I'm not familiar with code syntax, so it's hard going at this stage.

I need to break the code down line by line so I can make sense of it; especially "if/else if/elif & while statements.

That aside, thanks for all your help, Jackson : )

A.

Profile icon
[deleted]

@Airbus Great, just break everything down, We all start somewhere.