Ask coding questions

← Back to all posts
I need Help. Why is my Rock paper scissors game against the computer not working?
greatbear437 (5)

I think i finished everything that i needed to do, but i dont understand what i did wrong. Any help would be greatly appreciated!

Answered by Taonga07 (11) [earned 5 cycles]
View Answer
Commentshotnewtop
Taonga07 (11)

i would personally use this.
it might fix your issue

use this in your code.
there may be some errors as i have not check it.
but it makes your code more simpler
this is just a ruff guideline for your code please make it your own

if c == r: #check if input is the same as computer choice
    print("Draw!")

elif c == 'rock': 
    if r == 'scissors':
        print('Rock smashes Scissors! You Win!')
    else:
        print('Paper covers Rock! Sorry, You Lose!')

elif c == 'scissors': 
    if r == 'paper':
        print('Scissors cuts Paper! You Win!')
    else:
        print('Rock smashes Scissors! Sorry, You Lose!')

elif c == 'paper': 
    if r == 'rock':
        print('Paper covers Rock! You Win!')
    else:
        print('Scissors cuts Paper! Sorry, You Lose!') 

else:
    print('sorry you can not choose this')
'''

i would also recommend adding a while True loop so you not have to rerun your code each time and it will repeat your code forever.
Taonga07 (11)

this makes your code more simple by getting rid of unnecessary lines here is what i mean

if c == 'rock':
  if r == 'scissors':
    print('Rock smashes Scissors! You Win!')
  elif r == 'paper':
    print('Paper covers Rock! Sorry, You Lose! :(')
###########################
  elif r == 'rock':       #
    print("It is a tie!") #
###########################
elif c == 'paper':
  if r == 'rock':
    print("Paper covers Rock! You Win!")
  elif r == "scissors":
    print('Scissors cuts Paper. Sorry, You Lose! :(')
###########################
  elif r == 'paper':      #
    print("It is a tie!") #
###########################
elif c == 'scissors':
  if r == 'paper':
    print('Scissors cuts Paper! You Win!')
  elif r == 'rock':
    print("Rock smashes Scissors! Sorry, You Lose! :(")
###########################
  elif r == 'scissors':   #
    print("It is a tie!") #
###########################

the hashed boxes we can get rid of. for example the last one
the elif r == 'scissors': is in the if statement
elif c == 'scissors': so c == r
this is the same for all the hashed boxes and so we only need one line see my code

Taonga07 (11)

at the start of my bit of code you can see me checking if c and r is the same. therefore we do not need to worry about a third elif and as there is nothing else there you can just use else elif r == 'scissors': to else

the rest of the code is from yours except from the else i added at the end to check if you have not typed scissors, paper, rock

Taonga07 (11)

@greatbear437 thanks for selecting my awnser

ChezCoder (1484)

hmmm...

try checking your if statements to make sure they output the correct things, that will fix it.

greatbear437 (5)

@ChezCoder How would you do this? I am new to python.

ChezCoder (1484)

@greatbear437 hmm... remove the break in line 6

PDanielY (983)

You didn't do anything wrong. The program is running perfectly

greatbear437 (5)

Thank you @PDanielY for the quick response, but try the program out a few times again and again. You will see that is not working properly.

AmazingMech2418 (693)

In line 5, replace random.choice(t) with r. You already have a variable set, so you should use that instead of making another random choice.

CodeLongAndPros (977)

This would be a good time to run it through PDB

CodeLongAndPros (977)

@greatbear437 I would break at line 6, and pp some vars to get runtime info.

greatbear437 (5)

@CodeLongAndPros when i put break at line 6, it says break outside of loop and how do u pp variables?

CodeLongAndPros (977)

@greatbear437 In PDB, enter:

b 6
c

and type:

pp {var}

where var is a valid var.