Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment: Functions in Python Lab Assignment Prompt : Use Python functions to compare results in a guessing game In this assignment, you will create two

Assignment: Functions in Python

Lab Assignment Prompt: Use Python functions to compare results in a guessing game

In this assignment, you will create two functions and use them in your program. One function will perform the task of calculating the result of a 6-sided die toss. The other function will compare that result to a guess.

Three key program requirements:

1) The Die Toss Function

This function will imitate a 6-sided die being tossed and it will produce a random result. Function Requirements:

It will require no parameters.

There must be a docstring at the head of the function that describes in no more than one sentence what the function does.

It will produce a random number between 1 and 6 (inclusive) using an appropriate function from Pythons random module.

It will return this number as an integer value.

2) The Guessing Function

This function will compare a guess of the die toss result to the actual result and return the outcome.

Function Requirements:

It will accept two parameters as input:

1. The first parameter will be the users guess regarding the outcome of the die toss.

2. The second parameter will be the result of the die toss (1 6, inclusive). This parameter is to have a default value of 4.

There must be a docstring at the head of this function as well, which describes in no more than one sentence what the function does.

It will print an error message of your choosing if the user of your program enters a guess lower than 1 or greater than 6.

If there is an error message, the program should exit or gracefully end.

If no error, the parameters of this function will be compared.

1. If the two values are equal, the function will return a string that contains "matched".

2. If the two values are not equal, the function will return a string that contains "did not match".

3) Main Body of Program

In addition, write some lines of code that make use of your functions.

Main Body Requirements:

1. Create a variable named user_guess and set it equal to the input provided by the user of your program. You should prompt for this input. Example: Enter your guess for the die toss (1-6):

2. Ensure that this user input can be converted to an integer and if not, print an error message of your choosing and exit or gracefully end the program.

3. Create a second variable called die_toss_result and set this variable equal to the call of your Die Toss function.

4. Create a third variable named user_result and set it equal to the call of your Guessing function, using user_guess from Step 1 and die_toss_result from Step 2 as the arguments.

5. Print your die_toss_result variable.

6. Then print a message that looks like this:

Example: "I guessed and the die toss ."

REMINDER: Run your code and make sure it works as intended.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions