Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CSC 148 COMPUTER SCIENCE 1

Loop-free addition quiz (uses recursion)

Programming Assignment #3


For this assignment, you will write an "Addition Quiz" that quizzes the user on simple addition. This program must NOT use loops; all repetition will be done with recursion. The program will use the    random module to generate operands, global variables to as well as global constants, and several   different functions as well as a main() function.

Submit your work using the "Programming Assignment 3" Canvas Quiz. You must submit a copy of the program as well as a log showing your test runs of the program.

To submit the program code, save the .py file to your computer and then upload that file to Canvas where requested.  To submit the log file, use Options - > Save a Copy, save the .log file to your         computer, and then upload that file to Canvas where requested.

additionquiz.py

Write a Python program named additionquiz.py that prompts the user for a level (1=low, 2=higher,    3=highest) and then presents the user with 5 addition problems at that level, accepts an answer for    each, and tells the user if each answer is correct (and, if not, what the answer should be). After             displaying the five problems, the program asks the user whether they want to play again; if so, it uses recursion to repeat (indefinitely).

Recursion is used throughout the program. The program repetition is accomplished by using           recursion in the playgame() function.  The function get_int_input() is used in three different program locations; this function does recursive input checking.

Your program should include the following global constants and global variables:

# global constants

MIN_ANSWER = 0

MAX_ANSWER = 999999

#global variables

num_problems = 0

num_correct = 0


The global constants will be used to give a range for allowable answers to the math questions (since your get_int_input()function requires a minimum and maximum. The global variables will be     used to track the progress through the game so the results can be displayed at the end. Remember  that, to update global variables in a function, you must specify them as global in that function.


Your program must include the following functions:

1) main() :

This is the main function of your program. This function prints a welcome message and then calls playgame() to start the quiz.

2) playgame():

This is a recursive function that calls getlevel() to get the requested level and then           showproblems() to exhibit 5 problems at that level.  It then uses get_int_input () to   promptsthe user to either stop play or to play again. If the user chooses to play again, the     function calls itself recursively to play another round. If the user chooses to stop, it prints the score and a nice message thanking the user for playing.

3)  get_level ():

This function utilizes the get_int_input () function to prompt the user for one of the values 1, 2, or 3 and returns that value to the calling function. The levels refer to the size of the             operands that will be generated by addition_problem().

4)  showproblems (level):

This function calls addition_problem(), with the level value given, five times. Since we don’t use loops in this program, it is just 5 calls to addition_problem() in a row.

5) addition_problem(level):

This function uses random.randint() to get two operands in the correct range, then builds   a string to prompt the user for a numerical answer using get_int_input(). Note that you  must import the random module in your program. Use the global constants MIN_ANSWER and MAX_ANSWER as the minimum and maximum values passed to get_int_input(). The        prompt should be a string in the form “1 + 2 = “, built from the two operands as well as             addition, equals, and space characters.

At level 1, the operands will range from 1 to 10; at level 2, the operands will range from 10 to 100, and, at level 3, the operands will range from 100 to 1000 (yes, there is overlap).  Use        exponents with level to compute the ranges, rather than if/then statements.  In other words,

pass the minimum value of 10**(level- 1) and maximum value of 10**level to the random.randint() function.

Once an appropriate answer is retrieved from get_int_input(), this function compares the user’s answer to the correct answer (determined by adding the two operands together), prints   a response (with the correct answer as appropriate), and updates global variables                       num_correct and num_problems to keep track of the score in the game. Remember that, to update global variables, you must declare the variables as global in the function.

6)  get_int_input(prompt,min1,max1):

This is the recursive function that we worked on / will work on in class (2/7-8). It takes a


prompt, a minimum value, and a maximum value, and prompts the user recursively until the user enters a positive integer in the range given.

Below is a call-graph of this program:


Sample Run:

Because the program generates random operands, the actual problems should be different when you run your program. However, when you create your log, be sure to demonstrate the same features of   your own program, including non-numeric inputs and too-large inputs for your inputs, correct as well as incorrect answers, and repeated runs of the program at different levels.

In this sample, user input is shown in red type.

Welcome to addition quiz!

Which level? (enter 1, 2, or 3): 0

Value out of range!

Which level? (enter 1, 2, or 3): unknown

Invalid input!

Which level? (enter 1, 2, or 3): 10

Value out of range!

Which level? (enter 1, 2, or 3): 1

3 + 9 = 12

Correct!

7 + 9 = 16

Correct!

10 + 1 = 11

Correct!

2 + 3 = 5

Correct!

2 + 8 = 10

Correct!

Would you like to try some more problems? Enter 0 for no, 1 for yes: 1

Which level? (enter 1, 2, or 3): 2

34 + 89 = 123

Correct!

18 + 89 = 107

Correct!

55 + 96 = 0

Sorry, wrong answer! Correct answer is 151

23 + 76 = 99

Correct!

36 + 23 = no answer

Invalid input!

36 + 23 = -55

Invalid input!

36 + 23 = 9999999999999999999999

Value out of range!

36 + 23 = 0

Sorry, wrong answer! Correct answer is 59

Would you like to try some more problems? Enter 0 for no, 1 for yes: yes Invalid input!

Would you like to try some more problems? Enter 0 for no, 1 for yes: no Invalid input!

Would you like to try some more problems? Enter 0 for no, 1 for yes: 5

Value out of range!

Would you like to try some more problems? Enter 0 for no, 1 for yes: 1

Which level? (enter 1, 2, or 3): 3

284 + 146 = 430

Correct!

251 + 675 = 926

Correct!

895 + 583 = 1478

Correct!

987 + 528 = 1515

Correct!

681 + 674 = 1355

Correct!

Would you like to try some more problems? Enter 0 for no, 1 for yes: no Invalid input!

Would you like to try some more problems? Enter 0 for no, 1 for yes: 0

Your score is 13 correct out of 15.

Thank you for playing!

Challenge problem (optional):

If you finish quickly, challenge yourself by making a new version of your program that includes the   ability for the user to choose between addition problems and multiplication problems! There are no additional points given for challenge problems, but they can be fun and will give you additional        practice.


Welcome to math quiz!  Type 1 for addition, 2 Which level? (enter 1,

for multiplication: 2

2, or 3): 1


What to hand in:

Complete this assignment by uploading your program and log file as directed in the Programming3    Assignment "quiz" on Canvas.  Although your math problems will be different, you should exhibit the  input-checking features of all the various places where the user supplies input (choosing level,              answering questions, choosing whether or not to continue playing). Before running your tests, be sure to restart the shell to clear out any existing values or imports.

Your log file MUST match your program. If you change anything about your program, be sure to create a new log file.

In addition to uploading the files, you will be asked to answer some reflection questions about the assignment. Please answer all questions; this is part of the assignment.

HONOR CODE: Please remember that your work on this assignment must be your own. You may get  help from the TAs, and you may discuss the problem with other students or friends and family            members, but you may not share code or look at code written by anyone else (other than the sample programs from class or the textbook). In addition, you MAY NOT use internet resources such as          stackoverflow (garbage advice) or Chegg (blatant cheating). The honor code applies for all                  programming assignments.