Microsoft Word - CSC152-05_S19__Assignment_03.docx
SCSU Spring 2019 CSC 152‐05 Take Home Assignment 03
Points: 100
Due date: 04‐29‐2019 7:30pm.
Instructions: This is an individual assessment. Plagiarism will result in a zero grade.
Goal
To write a program in Python to test the user’s ability to do arithmetic operations on
fractions, and award points on co
ect attempts.
Description:
The program starts showing two messages to the user (see sample runs). It will have
one or more rounds, depending on whether the user decides to continue or not at
the end of each round. During each round, the program will ask the user to enter the
numerator and the denominator of the co
ect answer for an arithmetic operation
involving fractions. These fractions, as well as the specific operation itself (one of the
following four: addition, subtraction, multiplication, and division) should be
generated/chosen randomly. These fractions (operands) will always be positive,
however, please note that, the result might not be always positive (Note: You do NOT
need to reduce any of the fractions in this program to their lowest terms). The
operand fractions should always have a value less or equal to 1 when reduced (which
means the numerator of a fraction cannot be greater than the co
esponding
denominator). The range of values for the numerators of the fractions are
determined by the difficulty level chosen by the user (1, 2, or 3) at the beginning of
each round. Level 1 restricts the denominators to [1, 5], level 2 restricts the
denominators to [6, 10], and level 3 restricts the denominators to [11, 15]. Any other
value for the difficulty level entered by the user should be considered invalid and the
user should be asked to re‐enter the difficulty level. Once a problem (two
numerators, two denominators and the arithmetic operation to be performed on
them) is generated, the user is shown the problem and asked to enter the numerator
and the denominator of the estimated result (if the result is negative, enter a negative
numerator and positive denominator). If both of the entered values are co
ect
(matches the co
ect numerator and denominator), the user gains points equal to the
difficulty level chosen for the problem (the user has zero points when the game
starts). At the end of each round, the user is shown the total points so far. The user
is then asked whether to continue playing or not. If the user answers affirmatively
(using either uppercase or lowercase Yes or y), another round begins. If the user
wants to leave (indicated by case insensitive an input of No or n), the program shows
how many points the user has accumulated so far, and terminates showing the
parting message. For exact input prompt and output details, see the example runs.
Example runs:
****************************************
‐‐‐‐‐‐‐‐‐‐Welcome To Math DOJO‐‐‐‐‐‐‐‐‐‐
****************************************
‐‐‐‐‐‐‐‐We are Going to Do Some Arithmetic Challenges Today !‐‐‐‐‐‐‐‐‐
What Difficulty Level You Want to Play at [1/2/3] ? 3
12/13 ‐ 10/12 = ?
Enter the Numerator: 2
Enter the Denominator: 3
So
y, wrong answer. Co
ect answer is: 14/156
You have total 0 points
Do you want to continue playing [Yes/Y/No/N] ? n
‐‐‐‐‐You are leaving with 0 points‐‐‐‐‐‐
###################################
‐‐‐‐‐‐‐Goodbye from Math DOJO‐‐‐‐‐‐
###################################
****************************************
‐‐‐‐‐‐‐‐‐‐Welcome To Math DOJO‐‐‐‐‐‐‐‐‐‐
****************************************
‐‐‐‐‐‐‐‐We are Going to Do Some Arithmetic Challenges Today !‐‐‐‐‐‐‐‐‐
What Difficulty Level You Want to Play at [1/2/3] ? 1
2/2 / 4/4 = ?
Enter the Numerator: 8
Enter the Denominator: 8
Co
ect Answer ! You got 1 points !
You have total 1 points
Do you want to continue playing [Yes/Y/No/N] ? y
What Difficulty Level You Want to Play at [1/2/3] ? 2
8/10 ‐ 8/8 = ?
Enter the Numerator: ‐16
Enter the Denominator: 8
So
y, wrong answer. Co
ect answer is: ‐16/80
You have total 1 points
Do you want to continue playing [Yes/Y/No/N] ? 2
Invalid response.
Do you want to continue playing [Yes/Y/No/N] ? NM
Invalid response.
Do you want to continue playing [Yes/Y/No/N] ? y
What Difficulty Level You Want to Play at [1/2/3] ? 3
9/13 * 10/11 = ?
Enter the Numerator: 90
Enter the Denominator: 143
Co
ect Answer ! You got 3 points !
You have total 4 points
Do you want to continue playing [Yes/Y/No/N] ? y
What Difficulty Level You Want to Play at [1/2/3] ? 4
Invalid response.
What Difficulty Level You Want to Play at [1/2/3] ? 5
Invalid response.
What Difficulty Level You Want to Play at [1/2/3] ? 3
11/12 * 11/13 = ?
Enter the Numerator: 121
Enter the Denominator: 156
Co
ect Answer ! You got 3 points !
You have total 7 points
Do you want to continue playing [Yes/Y/No/N] ? n
‐‐‐‐‐You are leaving with 7 points‐‐‐‐‐‐
###################################
‐‐‐‐‐‐‐Goodbye from Math DOJO‐‐‐‐‐‐
###################################
****************************************
‐‐‐‐‐‐‐‐‐‐Welcome To Math DOJO‐‐‐‐‐‐‐‐‐‐
****************************************
‐‐‐‐‐‐‐‐We are Going to Do Some Arithmetic Challenges Today !‐‐‐‐‐‐‐‐‐
What Difficulty Level You Want to Play at [1/2/3] ? 1
1/1 + 1/2 = ?
Enter the Numerator: 3
Enter the Denominator: 2
Co
ect Answer ! You got 1 points !
You have total 1 points
Do you want to continue playing [Yes/Y/No/N] ? nm
Invalid response.
Do you want to continue playing [Yes/Y/No/N] ? n
‐‐‐‐‐You are leaving with 1 points‐‐‐‐‐‐
###################################
‐‐‐‐‐‐‐Goodbye from Math DOJO‐‐‐‐‐‐
###################################
Technical Requirements
1. Use the Python random li
ary to generate the numerators, denominators, and
the arithmetic operation to be performed on the fractions (hint: you have to use
the function ‘choice’ for one of these).
2. Use at least five functions (excluding main function). The functions should be as
follows:
a. A function named show_decorated_message. It should take the following
three parameters: the message to be displayed, the length of the bars
shown above and below the message, and the character used for these
ars. This function should not return anything
. A function named get_user_response. It should accept two parameters:
the prompt to be shown to the user, and the list of allowed (valid)
esponses. This function should handle all the user input in the program,
and return the validated user response to the caller.
c. A function named validate_response. It should take the following two
parameters: the response to be validated, and the set of allowed (valid)
esponses. This function returns True or False depending on whether the
esponse is valid or not.
d. A function named generate_problem. It should accept a single parameter:
the difficulty level. This function should generate the problem for the user
and return a tuple containing the description of the problem and the
co
ect answer to the caller.
e. A function named compute_co
ect_answer. It should accept five
parameters: the denominators and the numerators of the two fractions,
and the operation to be performed. It should return a tuple containing the
numerator and the denominator of the co
ect answer computed from the
parameters.
f. The main function. It should act as the glue and make calls to the
necessary functions to run the program.
3. All the user input should take place within the get_user_input function.
4. You do not have to handle malformed input (non‐numerical inputs where
numerical input is expected, and vice‐versa). However, you will have to handle
inputs that are outside valid set of inputs (e.g. anything except for 1,2, or 3 is
invalid for difficulty level input) and keep asking the user to re‐enter the input
until a valid input is entered.
5. Your program should have the same format and spacing for input prompt and
outputs as shown in the example runs.
6. Your program should be fully commented.
7. Use these formulae for computing the co
ect answers:
a. a
+ c/d = ( a*d + b*c )/(b*d)
. a
‐ c/d = ( a*d – b*c )/(b*d)
c. a
* c/d = (a*c)/(b*d)
d. a
/ c/d = (a*d)/(b*c)
Program Steps
Every program in this course will require you to work on three stages. The first stage is
writing test cases, the second stage is writing designs, and the third stage is writing the
code (implementation of the design). The first two stages are going to help you to
understand and test the program – I recommended you to complete them before writing
your code. The last stage represents the code for the problem and that is the only part
that you need to submit.
(0 Points) Step 1: Test Case Design
One of the common mistakes of for students is to think about the code as the first step
to answer a problem. This is wrong, it is too early for the student to think about the code
as the first step to find a solution for a specific problem, instead you should think about
how the program is supposed to behave and come up with a test plan for your program.
Without writing any line of code, think about the input of the program and the
expected output or how the program is going to behave for certain inputs (for
example, invalid responses for difficulty level or whether to continue playing,
inco
ect numerator, inco
ect denominator, etc.). As a first step for every program
is to make a test plan for the program.
(0 points) Step 2: Program Logic Design
Have a look over the program description and come up with pseudocode that
describes how the program is going to look.
Your first step to do that is create a python file name it
“YOURLASTNAME_CSC15205S19_PROGRAM_03.py”.
Replace the part YOURLASTNAME with your actual last name.
Write the program pseudocode (use plain English, not python code) as
comments inside your Python file. You should put # at the start of each line.
Write your name and date on the top of the file as comments.
After the # symbol, give every step of your design a number so you can refer
to this step later (such as 1,1.1,1.2,2, etc.).
You have to come up with a design that has at least 5 functions excluding the
main function.
The functions will have their independent numbering starting from 1 and a
name.
Whenever you use a function in other part of the design, mention it.
Once you done, save your work.
(100 Points) Coding
Use the program design that you came up with from the previous phase and write a
python code which will work as mentioned in the program description. Your program
should be as follows:
Go back to your design file –the one you made from the previous step ‐ which has
the name “YOURLASTNAME_CSC15205S19_PROGRAM_03.py”.
Now go over and write an equivalent python code line(s) under every design step.
Make sure that every variable that you will use will have a proper name which
eflects its task (This name should not be one character)
Your program should ask the user to enter the inputs using the format provided in
the sample run, which you can find on the first page.
When you have finished coding (implementing the design), test it with the inputs
you used in Step 1 to verify that the actual outputs match the expected outputs.
Once you are ready to submit the program, attach it to an email with the subject
“YOURLASTNAME_CSC15205S19_PROGRAM_03” and send it to me at
XXXXXXXXXX
__________________________________________________________________
BONUS: 30 Points
If you can show the complete history (round number, the problems, the co
ect
answers, user’s answers, points gained at that round, and the total points at
the end of that round) of the game by storing each of these in a separate
list (except for the round number) and showing to the user at the end of the
game, you will get 30 bonus points. (HINTS: YOU WILL HAVE TO USE
LISTS FOR THIS PART).
Sample Run with bonus:
****************************************
XXXXXXXXXXWelcome To Math DOJO----------
****************************************
XXXXXXXXXXWe are Going to Do Some Arithmetic Challenges Today !---------
What Difficulty Level You Want to Play at [1/2/3] ? 1
2/4 + 4/4 = ?
Enter the Numerator: 24
Enter the Denominator: 16
Co
ect Answer ! You got 1 points !
You have total 1 points
Do you want to continue playing [Yes/Y/No/N] ? y
What Difficulty Level You Want to Play at [1/2/3] ? 2
9/10 / 1/6 = ?
Enter the Numerator: 54
Enter the Denominator: 10
Co
ect Answer ! You got 2 points !
You have total 3 points
Do you want to continue playing [Yes/Y/No/N] ? 3
Invalid response.
Do you want to continue playing [Yes/Y/No/N] ? y
What Difficulty Level You Want to Play at [1/2/3] ? 3
2/12 / 3/12 = ?
Enter the Numerator: 24
Enter the Denominator: 35
So
y, wrong answer. Co
ect answer is: 24/36
You have total 3 points
Do you want to continue playing [Yes/Y/No/N] ? n
-----You are leaving with 3 points------
###################################
-------Goodbye from Math DOJO------
###################################
Round#0 Problem: 2/4 + 4/4 = ? Co
ect Answer: 24/16 User's Answer: 24/16 Points Gained in Round: 1 Total Points after Round: 1
Round#1 Problem: 9/10 / 1/6 = ? Co
ect Answer: 54/10 User's Answer: 54/10 Points Gained in Round: 2 Total Points after Round: 3
Round#2 Problem: 2/12 / 3/12 = ? Co
ect Answer: 24/36 User's Answer: 24/35 Points Gained in Round: 0 Total Points after Round: 3
__________________________________________________________________
Special Instructions:
1. Use main function:
a. All code should be inside “def main():” block or some
function ( except for import statements)
. Make sure to invoke the main function by including “main()”
2. Follow file naming convention as mentioned to avoid getting points
deducted.
Clues:
1. Return tuple containing tuples from a function:
def test_func():
a = 5
b = 6
c = 7
XXXXXXXXXXreturn (a, (b , c) )
2. Retrieve the values stored in the tuples:
(x, (y, z) ) = test_func()
3. Center justifying a string s to a longer string of length n and padding character c:
Use s.center(n, c). Left and Right justification can be done using the functions ljust()
and rjust(), which takes similar arguments as the function center().
https:
docs.python.org/3/tutorial/inputoutput.html
Examples:
"Hello".center(15,"-") produces '-----Hello-----'
"Welcome".center(20,"#") produces '######Welcome#######'