String input recognition in RPS

This is the place for queries that don't fit in any of the other categories.

String input recognition in RPS

Postby MGxD » Tue Jun 10, 2014 4:41 pm

Hi, I've just recently started learning to use python, but have run into a bit of a problem. In my rock, paper, scissors game, the user input is not matching to the "r", "p", and "s" strings that I've inputted (in the part #Player chooses). Could someone steer me in the right direction?

Code: Select all
# Rock Paper Scissors // Roshambo
# Compete against the computer, which picks randomly

import random

print "Welcome to Rock, Paper, Scissors! (or roshambo, if you prefer)"

player = raw_input("Enter your name: ")

# Number of rounds needed to win
winsNeeded = int(raw_input("How many points are required for a win? "))

playerWins = 0
pcWins = 0
playerChoice = None

pcTaunts = ("You'll never be the man your mother is.", "You must have been born on a highway because that's where most accidents happen.", "If you were twice as smart, you'd still be stupid.", "In the battle of wits, you fight unarmed.", "You have the face of a saint. Saint Bernard, that is.")

while (playerWins < winsNeeded) or (pcWins < winsNeeded):
   #Player chooses
   playerInput = raw_input("Choose (r)ock, (p)aper, or (s)cissors: ")
   if playerInput.lower() == "r":
      playerChoice = "rock"
   if playerInput.lower() == "p":
      playerChoice = "paper"
   if playerInput.lower() == "s":
      playerChoice = "scissors"
   else:
      print "Not valid"
   
   #Computer randomly chooses
   pcChoice = random.choice("rock, paper, scissors")
   
   #Win conditions
   if playerChoice == pcChoice:
      print "It's a draw."

   if (playerChoice == "rock") and (pcChoice == "paper"):
      print "Computer wins!"
      pcWins += 1

   if (playerChoice == "rock") and (pcChoice == "scissors"):
      print player, "wins!"
      playerWins += 1

   if (playerChoice == "scissors") and (pcChoice == "rock"):
      print "Computer wins!"
      pcWins += 1

   if (playerChoice == "scissors") and (pcChoice == "paper"):
      print player, "wins!"
      playerWins += 1

   if (playerChoice == "paper") and (pcChoice == "scissors"):
      print "Computer wins!"
      pcWins += 1

   if (playerChoice == "paper") and (pcChoice == "rock"):
      print player, "wins!"
      playerWins += 1

#Player victory
if playerWins == winsNeeded:
   print "Congratulations, %s, you beat the evil computer!" % player

#Computer victory
if pcWins == winsNeeded:
   print "%s, ", random.choice(pcTaunts) % player

raw_input("\n\nPress the enter key to exit")
Last edited by Yoriz on Tue Jun 10, 2014 5:12 pm, edited 1 time in total.
Reason: First post lock.
MGxD
 
Posts: 2
Joined: Tue Jun 10, 2014 4:32 pm

Re: String input recognition in RPS

Postby Yoriz » Tue Jun 10, 2014 5:12 pm

Welcome to the forum.
Please read the new user read this link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 833
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: String input recognition in RPS

Postby MGxD » Tue Jun 10, 2014 7:36 pm

So after putting more thought, I realized that the way I wrote the if statements made them seem separate. This was easily solved by replacing them with elif. This what my finished product looks like, if anyone is interested in giving me coding style tips.

Code: Select all
# Rock Paper Scissors // Roshambo
# Compete against the computer, which picks randomly

import random
import re

print "\nWelcome to Rock, Paper, Scissors! (or roshambo, if you prefer)"

player = raw_input("Enter your name: ")

# Number of rounds needed to win
winsNeeded = int(raw_input("How many points are required for a win? "))

playerWins = 0
pcWins = 0
playerChoice = None

pcTaunts = ("You'll never be the man your mother is.", "You must have been born on a highway because that's where \nmost accidents happen.", "If you were twice as smart, you'd still be stupid.", "In the battle of wits, you fight unarmed.", "You have the face of a saint. Saint Bernard, that is.")

while (playerWins < winsNeeded) and (pcWins < winsNeeded):
   #Player chooses
   playerInput = raw_input("Choose (r)ock, (p)aper, or (s)cissors (or (q)uit): ")
   if playerInput.lower() == "r":
      playerChoice = "rock"
   elif playerInput.lower() == "p":
      playerChoice = "paper"
   elif playerInput.lower() == "s":
      playerChoice = "scissors"
   elif playerInput.lower() == "q":
      break
   else:
      print "Not valid"
      playerInput = raw_input("Choose (r)ock, (p)aper, or (s)cissors: ")
   
   #Computer randomly chooses
   pcChoice = random.choice(["rock", "paper", "scissors"])

   print "\n%s: %s\tComputer: %s" % (player, playerChoice, pcChoice)
   
   #Win conditions
   if playerChoice == pcChoice:
      print "It's a draw."
      print "Score: %s %d\tComputer %d" % (player, playerWins, pcWins)

   elif (playerChoice == "rock") and (pcChoice == "paper"):
      print "Computer wins!"
      pcWins += 1
      print "Score: %s %d\tComputer %d" % (player, playerWins, pcWins)

   elif (playerChoice == "rock") and (pcChoice == "scissors"):
      print player, "wins!"
      playerWins += 1
      print "Score: %s %d\tComputer %d" % (player, playerWins, pcWins)

   elif (playerChoice == "scissors") and (pcChoice == "rock"):
      print "Computer wins!"
      pcWins += 1
      print "Score: %s %d\tComputer %d" % (player, playerWins, pcWins)

   elif (playerChoice == "scissors") and (pcChoice == "paper"):
      print player, "wins!"
      playerWins += 1
      print "Score: %s %d\tComputer %d" % (player, playerWins, pcWins)

   elif (playerChoice == "paper") and (pcChoice == "scissors"):
      print "Computer wins!"
      pcWins += 1
      print "Score: %s %d\tComputer %d" % (player, playerWins, pcWins)

   elif (playerChoice == "paper") and (pcChoice == "rock"):
      print player, "wins!"
      playerWins += 1
      print "Score: %s %d\tComputer %d" % (player, playerWins, pcWins)

#Player victory
if playerWins == winsNeeded:
   print "\nCongratulations, %s, you beat the evil computer!" % player

#Computer victory
if pcWins == winsNeeded:
   print "You lose, %s.\n" % player
   print random.choice(pcTaunts)

raw_input("\nPress the enter key to exit")
MGxD
 
Posts: 2
Joined: Tue Jun 10, 2014 4:32 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baldyr and 3 guests