import random def get_user_input(): # Asks the user for rock, paper, and scissors. user_weapon = raw_input("Please select your weapon: (R) for rock, (P) for paper, (S) for scissors, or (Q) to quit: ") # Turns it in to uppercase when the user's input is lowercase if user_weapon == "r": user_weapon = "R" elif user_weapon == "p": user_weapon = "P" elif user_weapon == "s": user_weapon = "S" elif user_weapon == "q": user_weapon = "Q" # Asks the user to enter again if none of the options is entered elif user_weapon != "R" and user_weapon != "P" and user_weapon != "S" and user_weapon != "Q": print "Error. Please enter again!" user_weapon = get_user_input() return user_weapon def choose_weapon(): # randomly chooses rock, paper or scissors for the computer com_no = random.randint(1, 3) if com_no == 1: computer_weapon = "R" elif com_no == 2: computer_weapon = "P" else: computer_weapon = "S" return computer_weapon def determine_winner(user_weapon, computer_weapon): # Compares the user_weapon to the computer_weapon and determines the winner if user_weapon == computer_weapon: winner = "T" elif (user_weapon == "R" and computer_weapon == "P") or (user_weapon == "P" and computer_weapon == "S") or (user_weapon == "S" and computer_weapon == "R"): winner = "C" else: winner = "U" return winner def weapon_to_str(weapon): if weapon == "R": return "Rock" elif weapon == "P": return "Paper" elif weapon == "S": return "Scissors" def main(): rock_count = 0 paper_count = 0 scissors_count = 0 tie_count = 0 com_count = 0 user_count = 0 print "Let's play Rock, Paper, Scissors!" user_weapon = get_user_input() while user_weapon != "Q": if user_weapon == "R": rock_count += 1 elif user_weapon == "P": paper_count += 1 else: scissors_count += 1 computer_weapon = choose_weapon() winner = determine_winner(user_weapon, computer_weapon) user_str = weapon_to_str(user_weapon) com_str = weapon_to_str(computer_weapon) if winner == "T": winner_str = "It's a tie!" tie_count += 1 elif winner == "C": winner_str = "The computer wins!" com_count += 1 else: winner_str = "You win!" user_count += 1 print "You have chosen", user_str, "and the computer chose", com_str, ".", winner_str if tie_count + com_count + user_count == 0: print "I'm sorry to see you go without playing!" else: print "I'm sorry to see you go! You've proven to be a worthy adversary." print "The number of rounds the computer has won:", com_count print "The number of rounds the user has won:", user_count print "The number of rounds that ended in a tie:", tie_count print "The number of times you selected Rock:", rock_count print "The number of times you selected Paper:", paper_count print "The number of times you selected Scissors:", scissors_count 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