Last active
April 29, 2017 11:07
-
-
Save odanga94/79878d8f77dd67b10ef0244d60c59b82 to your computer and use it in GitHub Desktop.
Codeacademy Python Lesson: Rock, Paper, Scissors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This is a Rock-Paper-Scissors game. It prompts the user to select either Rock, Paper or Scissors. The computer then randomly selects either rock, paper or scissors and then compares the user's choice and the computer's choice. Finally it determines if the user has won and informs the user of the outcome""" | |
from random import randint | |
from time import sleep | |
options = ["R", "P", "S"] | |
LOSE = "You lost!" | |
WIN = "You Win!" | |
def decide_winner(user_choice, computer_choice): | |
print("%s") %(user_choice) | |
print("Computer Selecting...") | |
sleep(1) | |
print("%s") %(computer_choice) | |
user_choice_index = options.index(user_choice) | |
computer_choice_index = \ | |
options.index(computer_choice) | |
if user_choice_index == computer_choice_index: | |
print("It's a tie!") | |
play_RPS() | |
elif user_choice_index == 0 and \ | |
computer_choice_index == 2: | |
print(WIN) | |
elif user_choice_index == 1 and \ | |
computer_choice_index == 0: | |
print(WIN) | |
elif user_choice_index == 2 and \ | |
computer_choice_index == 1: | |
print(WIN) | |
elif user_choice_index > 2: | |
print("Your option should not be greater than 2") | |
return | |
else: | |
print(LOSE) | |
def play_RPS(): | |
print("Rock, Paper, Scissors!") | |
user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors:") | |
sleep(1) | |
user_choice = user_choice.upper() | |
computer_choice = options[randint(0, len(options) - 1)] | |
decide_winner(user_choice, computer_choice) | |
play_RPS() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment