In: Computer Science
Repl.it
Python
Ch 07 #5: Rock, Paper, Scissors Modification
Programming Exercise 11 in Chapter 6 asked you to design a program that plays the Rock, Paper, Scissors game. In the program, the user enters one of the three strings - “rock”, “paper”, or “scissors” - at the keyboard. Add input validation (with a case-insensitive comparison) to make sure the user enters one of those strings only.
I demonstrated the implementation of the program here: https://repl.it/@profeldridge/week06assignment06part2of2#main.py
Using the repl.it link above, modify the program to implement a new function called “get_user_input_validated()” that performs input validation (with a case-insensitive comparison) to make sure the user enters one of those strings only.
Requirements
You must use the given function prototypes. You can add more functions if you like.
def get_user_input_validated()
Resources
Use the same logic and text that you used for the flowchart
The Python Survival Guide shows how to make a string lowercase: https://lrccd.instructure.com/courses/102062/discussion_topics/1168996
Example Runs:
Please enter your choice (rock/paper/scissors):
rocks
Sorry - that selection is not valid. Please enter the text 'rock', 'paper', or 'scissors'
peppers
Sorry - that selection is not valid. Please enter the text 'rock', 'paper', or 'scissors'
ROCK
Human: [ rock ] vs. Computer: [ paper ]
Sorry! You lost
Deliverables
Source Code File: Name your file main.py and submit to Canvas.
Be sure to follow a Python Style Guide:
Recommended (v2.0) https://lrccd.instructure.com/courses/102062/modules/items/5357574
Original (v1.0) https://lrccd.instructure.com/courses/102062/pages/python-code-style-guide?module_item_id=5176726
<<<<<<<<<<<< python code >>>>>>>>>>>
"""
Program Name: Rock, Paper, Scissors Game
Filename: main.py
Author: Professor Eldridge
Date: 2020-09-21
Assignment: Lecture Demo
Description: Book Programming Exercise #11
Sources: See https://lrccd.instructure.com/courses/102062/pages/python-code-style-guide
"""
from random import *
ROCK_INT = 1
PAPER_INT = 2
SCISSORS_INT = 3
ROCK_STR = "rock"
PAPER_STR = "paper"
SCISSORS_STR = "scissors"
def get_user_input_validated():
choice=["rock","paper","scissors"]
while True:
user_choice = input("Please enter your choice (rock/paper/scissors):")
if user_choice.lower() in choice:
break
else:
print("Sorry - that selection is not valid.",end="")
return user_choice.lower()
def convert_raw_to_const(raw_number):
"""
Module Name: convert_raw_to_const
Parameters: int -> raw_number is an integer between 1-3 (inclusive)
Description: Given an integer, return the corresponding description of rock, paper, scissors
"""
result = ""
if (raw_number == ROCK_INT):
result = ROCK_STR
elif (raw_number == PAPER_INT):
result = PAPER_STR
elif (raw_number == SCISSORS_INT):
result = SCISSORS_STR
else:
print("Error! Unknown raw_number:", raw_number)
return result
def compare_choices(human, computer):
"""
Module Name: compare_choices
Parameters: Two strings -> human choice and computer choice of "rock, paper, or scissors"
Description: Given the two choices, use the rules of the game to identify the outcome
"""
result = 0
if (human == computer):
result = 0
elif (human == ROCK_STR and computer == SCISSORS_STR or
human == SCISSORS_STR and computer == PAPER_STR or
human == PAPER_STR and computer == ROCK_STR):
# This block is the essential rules of the game
result = 1
else:
# Short cut: assume if not a tie & human didn't win, then computer won
result = -1
return result
def main():
"""
Module Name: main
Parameters: None
Description: Program entry point. Provides the program flow of execution
"""
# The game doesn't end if the outcome is a tie
keep_playing = True
while (keep_playing):
# Random choice of 1-3
computer_choice_raw = randint(1,3)
# Convert the random choice into a constant that can be compared with
# the user's choice
computer_choice = convert_raw_to_const(computer_choice_raw)
user_choice=get_user_input_validated()
print("Human: [", user_choice, "] vs. Computer: [", computer_choice,"]")
# Following standard rules of comparison, evaluate the human & computer choices
outcome = compare_choices(user_choice, computer_choice)
if (outcome == 0):
print("Tie! You will play again until there is a winner")
keep_playing = True
elif (outcome == 1):
print("Congratulations! You won")
keep_playing = False
elif (outcome == -1):
print("Sorry! You lost")
keep_playing = False
else:
print("Error! Unknown outcome:", outcome)
keep_playing = False
main()
<<<<<<<<<<<<< sample run and changes >>>>>>>>>>
def get_user_input_validated():
choice=["rock","paper","scissors"]
while True:
user_choice = input("Please enter your choice (rock/paper/scissors):")
if user_choice.lower() in choice:
break
else:
print("Sorry - that selection is not valid.",end="")
return user_choice.lower()
change in main()
sample result
<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>