Python Forum
Odd behavior with Rock Paper Scissor game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Odd behavior with Rock Paper Scissor game
#1
I am following a tutorial for a rock paper scissor game and I made some changes to it to stray from the tutorial. Everything works except for the fact that sometimes the random choice doesn't appear to pick a choice and the score is not tabulated.

Here's the code I have, please help me identify what is going wrong:

import random
import sys

print("Rock, Paper, Scissors")

wins = 0
losses = 0
ties = 0

while True:
    print('%s Wins, %s Losses, %s Ties' % (wins, losses, ties))
    while True:
        playermove = input(
            "Enter your move: r(ock), (p)aper, (s)cissors or q(uit): ")
        if playermove == "q":
            print("Thanks for playing!")
            sys.exit()
        if playermove == "r" or playermove == "p" or playermove == "s":
            break
        else:
          print("Please type a valid character.")

    if playermove == "r":
      print("Rock versus...")
    if playermove == "S":
      print("Scissors versus...")
    if playermove == "p":
      print("Paper versus...")

    choices = ("r", "s", "P")
    compmove = random.choice(choices)

    if compmove == "r":
      print("Rock")
    elif compmove == "s":
      print("Scissors")
    elif compmove == "p":
      print("Paper")

    if playermove == compmove:
      print("It is a tie!")
      ties += 1
    elif playermove == "r" and compmove == "s":
      print("You win!")
      wins += 1
    elif playermove == 'p' and compmove == 'r':
      print('You win!')
      wins += 1
    elif playermove == 's' and compmove == 'p':
      print('You win!')
      wins += 1
    elif playermove == 'r' and compmove == 'p':
      print('You lose!')
      losses += 1
    elif playermove == 'p' and compmove == 's':
      print('You lose!')
      losses += 1
    elif playermove == 's' and compmove == 'r':
      print('You lose!')
      losses += 1 
Reply
#2
Comparisons are case-sensitive. On lines 25 and 30, you've used capital letters, and everywhere else lower-case. That's going to cause problems. When the computer picks "P", none of the comparisons will match, and you don't have a else condition to catch that none of the other ones matched.
Reply
#3
Thank you. Not sure how I missed that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock Paper Scissors Project in Python ankitdixit 8 4,718 Feb-23-2024, 03:14 PM
Last Post: DPaul
  Trying to create a visual rock paper scissors game urmom33 1 976 Dec-03-2022, 09:12 PM
Last Post: deanhystad
  Rock paper scissors in python with "algorithm" Agat0 23 5,777 Mar-01-2022, 03:20 PM
Last Post: Agat0
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,492 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Trying to implement Best of 3 logic in rock paper scissors game ShAhCh 5 3,267 May-11-2020, 04:31 PM
Last Post: ShAhCh
  Rock Paper Scissors with dictionaries ewgreht 2 3,823 May-01-2020, 03:19 PM
Last Post: deanhystad
  Rock, Paper, Scissors.. Help..hidden bug xxunknownxx 4 2,605 Mar-19-2020, 02:46 AM
Last Post: jefsummers
  Problem with Basic Rock Paper Scissors code BirinderSingh 3 2,383 Sep-13-2019, 03:28 PM
Last Post: ichabod801
  Rock, Paper, Scissors Help jyou99 1 2,411 Mar-26-2018, 04:07 PM
Last Post: nilamo
  Rock Paper Scissors (Need help) Lukili 1 2,428 Jan-14-2018, 10:00 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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