Python Forum
I attempted to make a rock paper scissors bot for homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I attempted to make a rock paper scissors bot for homework
#1
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.
buran write Jun-19-2021, 06:15 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
      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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo homework - how make contrast stretching? tavadani 0 1,003 Jan-17-2022, 10:36 AM
Last Post: tavadani
Question Rock, paper, scissors spelling error banidjamali 6 3,169 Jan-19-2021, 02:51 PM
Last Post: banidjamali
  Rock, Paper, Scissors Game kramon19 2 5,319 Jan-10-2020, 08:18 AM
Last Post: perfringo
  I need help with a python Rock Paper Scissors game urgently. Riff_Raff 3 5,821 Dec-05-2018, 09:13 PM
Last Post: nilamo
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,198 Jul-21-2018, 07:32 PM
Last Post: EvanCahill
  Rock Paper Scissors Warmlawpk441 4 5,013 Oct-11-2017, 10:55 AM
Last Post: gruntfutuk
  Rock paper scissors game samiraheen 3 6,340 Oct-03-2017, 07:07 PM
Last Post: buran
  The Python Book altered rock paper scissors Python_Noob 0 2,908 Sep-18-2017, 06:13 AM
Last Post: Python_Noob
  HELP---problems with rock paper scissors games kalt91 2 4,110 Sep-15-2017, 04:51 PM
Last Post: micseydel
  Rock, Paper, Scissors game help.. hentera 3 5,032 May-19-2017, 10:56 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020