#Steph Herbers #Rock, Paper, Scissors import random #this class contains methods with the randomized computer opponent class computer: #randomly choses move with rock, paper, and scissors having equal chances of being played def play(self): playProb = float(random.random()) if 0 <= playProb < 0.33: return 'rock' elif 0.33 <= playProb < 0.67: return 'paper' else: return 'scissors' #this class contains methods that associate with the human user of the program class user: #gets player's move from user input def play(self): userChoice = input('Would you like to play Rock, Paper, or Scissors? ') if userChoice not in ['Rock','rock', 'r', 'R', 'Paper','paper', 'p', 'P', 'Scissors', 'scissors', 's', 'S']: print('That is an invalid choice!') else: if userChoice == 'Rock' or userChoice == 'rock' or userChoice == 'r' or userChoice == 'R': return 'rock' if userChoice == 'Paper' or userChoice == 'paper' or userChoice == 'p' or userChoice == 'P': return 'paper' else: return 'scissors' return None #Determines winner def getWinner(results): #list of possible results if results == ['rock', 'paper']: return 'paper', 'Paper beats Rock!' if results == ['paper', 'rock']: return 'paper', 'Paper beats Rock!' if results == ['paper', 'scissors']: return 'scissors', 'Scissors beats Paper' if results == ['scissors', 'paper']: return 'scissors', 'Scissors beats Paper' if results == ['rock', 'scissors']: return 'rock', 'Rock beats Scissors' if results == ['scissors', 'rock']: return 'rock', 'Rock beats Scissors' else: return 'draw', 'It is a Draw!' # return integer of the number of rounds the user would like to play def getRound(): #get integer of rounds from user, if an integeter is not imputed, it will return 1 try: numRounds = int(input('What is the number of rounds you would like to play? ')) except: return 1 return numRounds #plays one round of the game and prints statements of the results of the round def oneRound(userPlay, compPlay, userWins): results = [userPlay, compPlay] winner, statement = getWinner(results) print('You played: ' + userPlay + ' The computer played: ' + compPlay) print(statement) if winner == userPlay: userWins.append(userPlay) print("Congrats you won!") return 'user' if winner == compPlay: print("Sorry you lost!") return 'comp' else: return 'draw' #determines who won after all the rounds def finalWin(userWins, numRounds): winner = (numRounds // 2) if winner < (numRounds / 2): winner+=1 print("You won {} out of {} rounds".format(len(userWins), numRounds)) if len(userWins) >= winner: print('You beat the computer!') else: print('Sorry! The computer beat you') def main(): print('Welcome to Rock, Paper, Scissors!') userPlayer = user() compPlayer = computer() rounds = getRound() #keeps track of how many times the user wins userWins = [] #iterates through the number of rounds, repeats a round if a draw occurs i = 0 while i != (rounds): print() userPlay = userPlayer.play() compPlay = compPlayer.play() draw = oneRound(userPlay, compPlay, userWins) if draw != 'draw': i+=1 else: pass print() finalWin(userWins, rounds) if __name__ == '__main__': main()
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