Skip to content

Commit

Permalink
manual_rps.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Charanaicore authored Sep 28, 2023
1 parent 5d38190 commit 22f713b
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions manual_rps.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
import random # import random module to use choice() method

def get_computer_choice():
import random as rd
computer_choice = rd.choice(["Rock" , "Paper", "Scissors"])
return computer_choice
'''Returns a random choice - either 'rock', 'paper', or 'scissors'.'''
# use random.choice() to randomly select and return one of the strings
return random.choice(['rock', 'paper', 'scissors'])

def get_user_choice():
user_choice = input("Choose Paper, Scissors or Rock:")
return user_choice
'''
Returns user choice of either 'rock', 'paper', or 'scissors'.
Takes user input and checks that it is valid. Will repeatedly ask for input if user does not input 'rock', 'paper' or 'scissors'.
Converts user input to lowercase, so capitalization is fine.
'''
# ask user to input their choice and convert to lowercase
user_choice = input("Please enter your choice of rock, paper or scissors: ").lower()
# start loop
while True:
# check if user input is valid
if user_choice in ['rock', 'paper', 'scissors']:
# if so, end loop and return user_choice
return user_choice
else:
# otherwise, ask for input again and convert to lowercase
user_choice = input("Sorry, that isn't a valid input. Please enter either rock, paper or scissors: ").lower()

def get_winner(computer_choice, user_choice):
'''
Takes two strings, compares them and evaluates who the winner is based on the rules of rock-paper-scissors.
def get_winner(computer_choice , user_choice):
if computer_choice == user_choice:
return print("It is a tie!")
elif computer_choice == "Rock" and user_choice == "Scissors":
return print("You lost")
elif computer_choice == "Paper" and user_choice == "Rock":
return print("You lost")
elif computer_choice == "Scissors" and user_choice == "Paper":
return print("You lost")
else:
return print("You won!")
Parameters
----------
computer_choice : str
A string generated from running the function get_computer_choice
user_choice : str
A string generated from running the function get_user_choice
Returns
-------
None
'''
# check all three conditions whereby computer can win
if (computer_choice == 'rock' and user_choice == 'scissors') or \
(computer_choice == 'paper' and user_choice == 'rock') or \
(computer_choice == 'scissors' and user_choice == 'paper'):
# if so, print losing message to console
print("You lost")
# check if user_choice and computer_choice are identical
elif computer_choice == user_choice:
# if so, print tie message to console
print("It is a tie!")
else:
# otherwise, print winning message to console
print("You won!")

def play():
get_winner(get_computer_choice(),get_user_choice())
'''Runs one round of the game rock-paper-scissors.'''
get_winner(get_computer_choice(), get_user_choice())

# run play() function to test code
play()

0 comments on commit 22f713b

Please sign in to comment.