Pa2 (Ab)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Sultan Qaboos University

College of Science - Department of Computer Science


COMP2101: Introduction to Computer Science – Spring 2024

Programming Assignment 2: Python Math Quiz


Due date: Saturday 6th April 2024 before 23:59 (through Moodle)

The purpose of this assignment is to practice problem solving, and programming in Python using variables,
arithmetic/relational/Boolean expressions, conditional statements, repetition statements, and functions, to develop a solution
to the given problem.

1. Codes of Ethics and Late Submission Policy:


Late Submission Policy:
 1-24 hours: 25% of the mark will be deducted.
 > 24 hours: Not accepted.
Code of ethics: It is essential that each student solve all programming assignments, lab tests and exams individually unless
instructed otherwise, e.g., for group projects. Copying, plagiarism, collusion, switching, and falsification are violations of the
university academic regulations. Students involved in such acts will be severely penalized. The department has adopted a
firm policy on this issue. A zero mark will be assigned the first time a student is caught involved in copying and his/her name
will be added to a watch list maintained by the Head of Department. Further repeated involvements in copying will cause the
student to get an F grade in that course. This is in line with the university academic regulations.

2. Deliverable
Create a Python program as your solution to the given problem and name the program file: pa2_xxxxxx.py. For instance, if
your ID is 38000, then you should submit a file pa2_38000.py. Your Python file will consist of two parts:
(i) Header comments that include:
✔ Documentation with your name, your student id, and section number.
✔ The main purpose, input, output and the algorithm in pseudocode.
✔ Test cases: provide at least four test cases testing different scenarios of the execution of your program.
(ii) Python program.

3. Grading Table:
Item Mark
Student Info comments 0.5
Purpose, Input, Output 1.5
Algorithm 10
Test cases(at least 4 cases) 2
Programming Style: Naming in accordance to Python convention 1
Function main(): comments, header, body, call 12
Function userInfo(…):comments, header, body, call 6
Function printHeader(…):comments, header, body, call 4
Function createExpression (…):comments, header, body, call 7
Function generateExpression (…):comments, header, body, call 10
Function getRandomOperator (…):comments, header, body, call 5
Function evaluateExpression (…):comments, header, body, call 10
Function evaluateOperation (…):comments, header, body, call 10
Function getPrecedence (…):comments, header, body, call 7
Function printReport (…):comments, header, body, call 12
Debugging: Program compiles and runs without errors 1
Proper File naming & submission 1
Total 100

1
4. Problem Definition
Write a Python program that simulates an online Python quiz presenting ten questions of randomly generated arithmetic
expressions. Each arithmetic expression consists of three integer numbers (operands) separated by two arithmetic operators.
Thus, an arithmetic expression has the following format:
N op N op N
where N is an operand, an integer number that is generated randomly from the range 0 to 10 inclusive, and op is an operator
that is generated randomly from the set of Python operators (+, -, *, /, //, %). The program should compute the result of the
expression formed by the generated operands and operators according to the rules of the operator precedence. For example,
if the expression is 5 + 7 / 3, the division operation should be evaluated before the addition operation. The program presents
the expressions to the user as questions, reads user answer and computes the quiz score. If the quiz score is less than 10, the
user is asked if wanting to resit the quiz in a second attempt. If the quiz is performed in two attempts the highest score is
selected. The program ends by presenting the user with a quiz report showing the user information, the quiz score and some
feedback (See the Sample Runs section).

5. Program Requirements:
- Use meaningful variable names, and provide comments describing your code and functions, including function comments
showing the purpose of each function, parameters and return values if any.
- Apply algorithmic thinking and step-wise refinement to develop your solution to the problem.
- Lists are not allowed in this assignment. Use strings where applicable.
- Global variables are not allowed in this assignment. Decide on appropriate function parameters and return values.
- You may use the built-in function type() to check the type of the expression result.
- Your program should include at least the following functions with suitable parameters and return types, as described below:
 main():
 Reads the user name and university id using function userInfo (…).
 Greets the user with his/her name and indicate the start of the quiz.
 Prints the quiz header information using function printHeader(…).
 Loops ten times creating an expression using function createExpression(…), presenting the returned expression as
a question to the user, receiving user answer, and comparing the user answer to the result returned by the function
createExpression(…). If the user answer is correct then it increments the user score, otherwise it displays the message
‘Wrong Answer” and shows the correct answer.
 Upon answering the ten questions, the function should give the user the option to resit the quiz in a second attempt
if the first attempt results in a score less than 10/10, then take the highest of the two attempts’ scores.
 Finally, it prints the quiz report using function printReport(...).
 userInfo(…):
 Repeatedly asks the user for name and university id until values are entered.
 Returns the user name and the university id
 printHeader(…):
 Prints on screen the information shown on the quiz header. See the Sample Runs section.
 createExpression():
 Loops generating an expression using function generateExpression(…) and evaluates the generated expression using
function evaluateExpression(…) until the generated expression evaluates to a numeric value; float or integer. If the
generated expression does not form a valid expression (i.e. evaluates to “Division by zero’ or ‘Modulus of Zero’),
then a new expression is generated until a valid expression that evaluates to a numeric integer of float value is
generated. For instance, the expression 2 – 5 % 0 evaluates to ‘Modulus of zero’, the expression 4 // 0 / 10 evaluates
to ‘Division by zero’, and the expression 9 / 3 / 0 evaluates to ‘Division by zero’. Such expressions should be ignored
and a new expression is generated until a valid expression is generated.
 The function returns the generated valid expression as a string and the result of the evaluation returned by the
function evaluateExpression

 generateExpression():
 Generates three random integers, between 0 and 10, representing the three operands (n1,n2, and n3) of the
expression.
 Generates two operators, op1 and op2, using function getRandomOperator().
 Returns the three operands n1, n2, and n3 and the two operators op1 and op2.

 getRandomOperator():
 Returns a random operator from the set of Python operators (+, -, *, /, //, %). Hint: Use a string to store the characters
of Python operators. Then select a character based on a random index.
2
 evaluateExpression(…):
 Receives the three operands (n1, n2, n3) and the two operators (op1 and op2) and returns the result of the expression.
 It calls getPrecedence(…) and evaluateOperation(…) functions.
 evaluateOperation(…):
 Receives two numbers and an operator (e.g. +, -, *, /, //,%), and returns the result of the operation that might be a
numeric value or a ‘Division by zero’, or a ‘Modulus of zero’. For instance, if the function receives two numbers 2
and 0 and the operator ‘%’ it should return ‘Modulus of zero’. Similarly, if the function receives two numbers 2 and
0 and the operator ‘/’ it should return ‘Division by zero’. If the function receives two numbers and operator that
evaluate to a valid numeric value, it evaluates the operation and returns the result of evaluation. For expressions
evaluating to float values, the value should be rounded to 1 decimal place according to Python rounding rules 1.

 getPrecedence(…):
 Receives an operator and returns its precedence as a positive number. The operators *, /, //, and % should have the
same precedence number which is higher than that for the operators + and -.
 printReport(…):
 Receives the user name, university id, and the quiz score and prints a quiz report showing the user name, university
id, quiz score and quiz feedback. The quiz feedback is based on the score as follows (See sample runs files):
Score Feedback
10 Excellent
8 to 9 inclusive Very Good
5 to 7 inclusive Good
Less than 5 Talk to your instructor

6. Sample Runs: (Examine more sample runs in the file pa2_MoreSampleRuns.txt on Moodle)
Sample Run 1:

1
Python rounds to the nearest even number when a number is exactly halfway between two others
For example: 2.25 rounds to 2.2 whereas 2.75 rounds to 2.8
3
Sample Run 2:

4
5
6

You might also like