Question

In: Computer Science

Repl.it Python Ch 07 #5: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked...

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

  1. 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

Solutions

Expert Solution

<<<<<<<<<<<< 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

<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>


Related Solutions

Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: 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. Modifications: Allow the user to input "r", "p", "s" or the full strings "Rock",...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming...
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming 1) Develop a function that prompts the user to enter their choice (1=Rock 2=Paper 3=Scissors) Return either a 1, 2, or 3 depending on the value the user has entered Do not continue the program until the user has entered a valid choice of 1, 2, 3 2) Develop a function that generates the computer player's choice Return either a 1, 2, or 3...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
please show me step by step? Chapter 11 Exercise 11-8 Volume Trade-Off Decisions [LO11-5, LO11-6] Barlow...
please show me step by step? Chapter 11 Exercise 11-8 Volume Trade-Off Decisions [LO11-5, LO11-6] Barlow Company manufactures three products—A, B, and C. The selling price, variable costs, and contribution margin for one unit of each product follow: Product A B C Selling price $ 180 $ 270 $ 240 Variable expenses: Direct materials 24 80 32 Other variable expenses 102 90 148 Total variable expenses 126 170 180 Contribution margin $ 54 $ 100 $ 60 Contribution margin ratio...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However,...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects...
Chapter 11 5) In the long run, all costs are variable costs. Why? 6) What is...
Chapter 11 5) In the long run, all costs are variable costs. Why? 6) What is the long-run average cost curve? What are the three ranges of output and in what order do they occur? Briefly define each of the three ranges. 7) What are economies of scale? What is the main source of economies of scale? 8) What are the diseconomies of scale and why might they occur?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT