"""Tracy Goldman, Girls Who Code 2018 Summer Immersion Program Teaching Assistant Application""" import random class RPS(object): """An instance is an RPS object, representing one of rock, paper, or scissors. Attributes: name [string]: the name that defines it ("ROCK", "PAPER", or "SCISSORS"). beats [string]: the name of the object that it defeats, following normal RPS rules. loses [string]: the name of the object that it loses to, following normal RPS rules. """ """Initializes an RPS object.""" def __init__(self,name): assert name == "ROCK" or name == "PAPER" or name == "SCISSORS" self.name = name if name == "ROCK": self.beats = "SCISSORS" self.loses = "PAPER" elif name == "SCISSORS": self.beats = "PAPER" self.loses = "ROCK" elif name == "PAPER": self.beats = "ROCK" self.loses = "SCISSORS" """Runs the RPS game. The player is prompted to select rock, paper, or scissors.""" def selection(): valid = False pscore = 0 oscore = 0 while not valid: choice = input("Rock, Paper, or Scissors? ") type(choice) newchoice = choice.upper() if newchoice == "ROCK" or newchoice == "PAPER" or newchoice == "SCISSORS": print("You chose " + newchoice) valid = True opchoice = opponentChoice() print("Opponent chose " + opchoice) comp = comparator(newchoice,opchoice) if comp == -1: print("You lose.") oscore += 1 elif comp == 0: print("Tie.") elif comp == 1: print("You win!") pscore += 1 print("Your score: " + str(pscore)) print("Opponent's score: " + str(oscore)) playagain = input("Play again? ") playagain = playagain.upper() if playagain == "YES" or playagain == "Y": valid = False else: if pscore > oscore: print("You win!") elif pscore < oscore: print("You lose.") else: print("Invalid choice.") """Selects rock, paper, or scissors for the opponent.""" def opponentChoice(): options = ["ROCK","PAPER","SCISSORS"] opchoice = random.randrange(0,3) return options[opchoice] """Compares the selections for the player and the opponent, and determines which object wins.""" def comparator(choice1, choice2): playerchoice = RPS(choice1) opchoice = RPS(choice2) if playerchoice.name == opchoice.name: return 0 elif playerchoice.beats == opchoice.name: return 1 elif playerchoice.loses == opchoice.name: return -1 if __name__ == '__main__': selection()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question