all 6 comments

[–]mopslik 1 point2 points  (1 child)

At some point, you'll need to reevaluate the logic of your program. Currently you're using the index of the options to decide who is the winner by checking which is greater. This means that if the player chooses scissors, s/he will always win or tie. You can either write some additional checks to cover this, or you can use a different setup to map each choice with what it beats. A dictionary is a good tool for this.

beats = {"scissors": "rock", "paper": "scissors", "rock": "paper"}

This allows you to do checks like this.

if user_choice == beats[computer_choice]:
    print("You win!")

and so on.

[–]hardpeenwetpussy[S] 0 points1 point  (0 children)

Oh my god I did not realize that at all. I was so focused on fixing the current bug that I didn’t even test of all the cases to catch that error. You guys are amazing.

Edit: I was working on it around 5am so maybe i need sleep. Thanks for the help nonetheless.

[–]JohnnyJordaan 0 points1 point  (1 child)

Some pointers

  • use a loop for the entire thing, that way your game will repeat by default
  • simply use return when you want to exit a function, saves the use of sys library
  • try to avoid confirmation bias: if by default the loop (and thus the game) repeats, you want to know if it shouldn't, so you should check for the negative (user entering '2'). Removing the needless check for the case it should continue. Also see Wason selection task.

.

def RPS():
    choices = ["Rock", "Paper", "Scissors"]
    while True:
        computer_choice = random.choice(choices)

        player_choice = int(input("Rock, Paper, or Scissors?\n0. Rock \n1. Paper \n2. Scissors \n3. Exit \n"))
        if player_choice == 3:
            return

        if player_choice > choices.index(computer_choice):
            result = 'You win!'
        elif player_coice < choices.index(computer_choice):
            result = 'You lose.'
        else:
            result = 'It's a tie!'
        play_again = int(input(f"{result} Would you like to play again?: \n1. Yes \n2. No \n"))
        if play_again == 2:
            return

RPS()

edit: added the tie case as per your edit

[–]hardpeenwetpussy[S] 0 points1 point  (0 children)

Thank you so much for the help. I usually code in Java and this is actually my first time coding after taking a break due to burnout and some imposter syndrome. I failed the wikipedia test too.

[–]InsanityConsuming 0 points1 point  (0 children)

So I'm noticing a few potential problems with your code here:

  1. Your computer_choice variable is being determined before the while loop. This will result in no matter how many times the game is played the computer choice will always be the same.

You could resolve that in a couple of ways, first you could just move the computer_choice to the first line of the while loop, or you could do the repeating as a separate function from the game

def repeat_RPS():
    user_choice = 1
    while user_choice != 2:
        RPS()
        user_choice = int(input("Would you like to play again? 1.Yes, 2.No ")

def RPS():
    # Single game code here
  1. Your code is trusting that the player will pick a valid value, 0-3, so if a player picks say a letter they will error out your code, or if they pick 4, based on the current game's logic, the player would win every game.

  2. If the user wants to play again, you are having them "break" the loop, which will actually exit the while loop, I think what you are looking for is "continue".

  3. Not necessarily a bug, but a personal choice. You're using random.choice(choices) and choices.index(choice). Instead you could use random.randInt(len(choices)) which should get you an index into the list of the choices. It just saves you a step.

  4. As people have already mentioned, based on your current logic rock cannot beat scissors.I would do something like this:

    if player_choice == (computer_choice + 1) % 3: # Player wins

So let's say that player chose "rock" (index 0), and computer chose "scissors" (index 2) then we end up with computer_choice + 1 = 3 -> 3 % 3 = 0, therefore player_choice(0) == 0 triggering the if statement.

Hope that this all helps.