Python Forum

Full Version: I attempted to make a rock paper scissors bot for homework
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For a homework assignment, I'm making a rock paper scissors bot that plays against you. However it has one particular bug. Here is the code in its entirety.
import random
def RPS():
    R="rock"
    P="paper"
    S="scissors"
    player_choice=input("What do you want to play? R/P/S")
    throw_choice=[R,P,S]
    computer_choice=random.choice(throw_choice)
    if player_choice=="R":
        print("You played rock, and the computer played "+computer_choice+".")
    elif player_choice=="P":
        print("You played paper, and the computer played "+computer_choice+".")
    elif player_choice=="S":
        print("You played scissors, and the computer played "+computer_choice+".")
    if computer_choice==player_choice:
        return "T"
    elif computer_choice==R:
        if player_choice=="P":
            return "W"
        elif player_choice=="S":
            return "L"
    elif computer_choice==P:
         if player_choice=="R":
            return "L"
         elif player_choice=="S":
            return "W"
    elif computer_choice==S:
         if player_choice=="R":
            return "W"
         if player_choice=="P":
            return "L"
def play_RPS():
  continue_playing="Yes"
  win_count=0
  loss_count=0
  tie_count=0
  while (continue_playing=="Yes"):
    continue_playing=input("Do you want to continue playing? Yes/No ")
    if continue_playing=="Yes":
      RPS()
      if RPS()=="W":
          print("You won!")
          win_count=win_count+1
      elif RPS()=="L":
          print("You lost...")
          loss_count=loss_count+1
      elif RPS()=="T":
          print("You tied.")
          tie_count=tie_count+1
    print("Here are the results!")
    print("Wins:"+str(win_count))
    print("Losses:"+str(loss_count))
    print("Ties:"+str(tie_count))
play_RPS()
What it is supposed to do is first ask you if you want to continue playing the game, and then it asks you what you want to play, and then the computer randomly picks what to play. It then is supposed to reveal the result of the round before asking you if you want to continue playing again, and so on. However, depending on what I pick, the segment where the computer asks for the player's throw loops a different number of times depending on what I pick. I have no idea what is causing this bug and I would very much like advice. Also for some reason the indents don't show up on the post so just pretend like they're there.
Hi, as BURAN said, put your code between Python tags .
Otherwise it's anybody's guess what is going on.
At first sight though, you seem to use the +/- same lines of code
over and over again. That calls for simplification Smile
Paul
      RPS()
      if RPS()=="W":
          print("You won!")
          win_count=win_count+1
      elif RPS()=="L":
          print("You lost...")
          loss_count=loss_count+1
      elif RPS()=="T":
          print("You tied.")
          tie_count=tie_count+1
RPS is called and the returned result is not saved to a variable
In all the if/elif statements a new RPS is called each time instead of using the first RPS returned result variable.
This is a good program to demonstrate the power of dictionaries. Other than calling RPS() four times as often as you should, there is nothing wrong with your program, but it could be made shorter if you replaced your individual variables with dictionaries. You can add some nice features at the same time.
import random

RULES = {'R':('S', 'Rock breaks Scissors'), 'P':('R', 'Paper covers Rock'), 'S':('P', 'Scissors cuts Paper')}
PLAYS = list(RULES.keys())

def RPS():
    results = {'Win':0, 'Lose':0, 'Tie':0}
    play = None
    while play != 'Q':
        play = input("Choose R(ock)/P(aper)/S(cissors)/Q(uit): ").upper()
        if play in PLAYS:  # If the input is valid
            computer = random.choice(PLAYS)
            if play == computer:
                print("You tied!")
                results['Tie'] += 1
            elif RULES[play][0] == computer:  # Did you win?
                print(f'{RULES[play][1]}.  You win!')
                results['Win'] += 1
            else:
                print(f'{RULES[computer][1]}.  You lose')
                results['Lose'] += 1
    for result in results.items(): # Print results
        print(*result)

RPS()
RULES contains the rules for winning Rock Paper Scissors. 'R' beats 'S', 'P' beats 'R' and 'S' beats 'P'. I think this is a clearer description of the game rules than a bunch of if statements. The dictionary also contains a message for each rule which can be printed in the results after each round. Putting this information in a dictionary a replaces several if statements with a couple lines of code.