Question

In: Computer Science

TheMathGame Develop a program in PYTHON that will teach children the basic math facts of addition....

TheMathGame

Develop a program in PYTHON that will teach children the basic math facts of addition. The program generates random math problems for students to answer. User statistics need to be kept in RAM as the user is playing the game and recorded in a text file so that they may be loaded back into the program when the student returns to play the game again. In addition, the youngster should be allowed to see how (s)he is doing at any time while (s)he is playing the game.

PROGRAM REQUIREMENTS

1. Generate simple addition problems

a. the total must be an integer > 0

b. the addends must be randomly generated

c. the addends must range in value from 1 to 10, inclusive

2. Validate user input at every opportunity.

3. The program should keep track of the following statistics while the user is playing the game: 1. The total correct answers 2. The total wrong answers

4. A separate text file must be created for every user:

a. Statistics are read from the file at the start of the game (if the user chooses to “Read Stats from file”).

b. Statistics are recorded at the end of every game (if the user chooses to “Write Stats to file”).

5. The program must be developed using the following functions: displayMenu(),promptUser(),generateAddition(),readFromFile(),writeToFile(), and validateUserAnswer(). However, you may add your own functions as necessary. Read below for a detailed description of each function.

6. The displayMenu() function must display the following menu: 1. Generate Addition Problem 2. Read Stats from file 3. Write Stats to file 4. Display Stats 5. End

7. The promptUser() function must prompt the user for a menu option AND validate it; only positive integers from 1-5 should be allowed. This function must return the validated user choice to the caller.

8. The generateAddition() function must generate and display an addition problem with integer addends ranging in value from 1 – 10 inclusive. This function should also compute the correct answer to the problem and return it to the caller.

9. The readFromFile() function must prompt the user for a name and if the text file exists, then read the totalCorrect and totalWrong stats from the file and return to the caller BOTH the totalCorrect, totalWrong values. If the file does not exist, then a message indicating that it does not exist must be displayed to the user.

10. The writeToFile() function must take two parameters; the totalCorrect and the totalWrong and save the stats to a text file. It is important that the text file created MUST have the same name as the user.

11. The validateUserAnswer() function is responsible for validating the user response/answer to the addition problem. This function needs to ENSURE that user input is valid; i.e., an integer and return the validated integer.

12. The main() function puts it all together.

Restrictions: 1. No Global variables 2. No labels or go-to statements 3. No infinite loops in your programs; these are loops that do not have a definite logical expression of other condition to signal when the loops ought to terminate. 4. No break statements to exit loops. 5. The program must not crash for any reason.

Solutions

Expert Solution

Raw_code:

def displayMenu():
print("1. Generate Addition Problem")
print("2. Read Stats from file")
print("3. Write Stats to file")
print("4. Display Stats")
print("5. End")

def promptUser():
option = int(input("Enter an option from 1 to 5: "))
# while loop for input error checking
while(option < 1 or option > 5):
print("Invalid option")
option = int(input("Enter an option from 1 to 5: "))
else:
return option

def generateAddition():
# importing random module
import random
#generating random integer b/w 1 and 10 inclusively
addend1 = random.randint(1, 10)
addend2 = random.randint(1, 10)
print(f"{addend1:1d} + {addend2:1d} = ")
return addend1 + addend2

def readFromFile():
name = input("Enter your name: ")
file_name = name + ".txt"
# using try block to find whether the file exists or not
try:
with open(file_name, "rt") as file:
data = file.read().split()
(totalCorrect, totalWrong) = tuple(data)
return (int(totalCorrect), int(totalWrong))
except FileNotFoundError as err:
print("It does not exist!")

def writeToFile(totalCorrect, totalWrong):
name = input("Enter your name: ")
with open(name + ".txt", "wt") as file:
file.write(str(totalCorrect) + " " + str(totalWrong))
  
def validateUserAnswer():
# using try statement for user input validation
try:
answer = int(input("Enter an answer: "))
except ValueError as err:
print("It is not an integer!")
answer = validateUserAnswer()
finally:
return answer

# main function
def main():
  
option = 0
answer = 0
totalCorrect = 0
totalWrong = 0
# initially creating a file with name as username,
# since user first option may 2 (read stats from file)
writeToFile(totalCorrect, totalWrong)
# while loop runs until user end the game
while(option != 5):
# calling displayMenu() function
displayMenu()
# calling promptUser() function
option = promptUser()

# checking user option
if option == 1:
# calling generateAddition function
answer = generateAddition()
# and validating the user answer
if answer == validateUserAnswer():
# if user answer correct, incrementing totalCorrect by 1
totalCorrect += 1
else:
# if user answer is wrong, incrementing totalWrong by 1
totalWrong += 1
elif option == 2:
#note: calling readFromFile always returns the totalCorrect and totalWrong stored
# at the start of game, by calling writeToFile() will update the data
totalCorrect, totalWrong = readFromFile()
elif option == 3:
writeToFile(totalCorrect, totalWrong)
elif option == 4:
print("Total Correct = {0:1d}; Total Wrong = {1:1d}".format(totalCorrect, totalWrong))
print()

#calling function
main()

# file name is taken in writeToFile() function because there is no way to write to a file without knowing the required file name


Related Solutions

There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
Project Outcomes: Develop a python program that uses:  decision constructs  looping constructs  basic...
Project Outcomes: Develop a python program that uses:  decision constructs  looping constructs  basic operations on an list of objects (find, change, access all elements)  more than one class and has multiple objects Project Requirements: 1. Develop a simple Hotel program. We will have two classes, a Hotel class representing an individual hotel and a Room class. The Hotel class will contain several Room objects and will have several operations. We will also have a driver program...
Look online- find a website that inspires you to teach children about math and science. Post...
Look online- find a website that inspires you to teach children about math and science. Post the URL address in your response and then tell us what kind of activities this site inspires. What would be one activity you would do with young children to support their math and/or science development?
Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
IN PYTHON Develop a program in python that includes a number of functions for the multi-server...
IN PYTHON Develop a program in python that includes a number of functions for the multi-server queue. The program accepts arrival and services rates and the number of servers and calls your functions to output the average number of customers and average waiting time in the system.
Write a program named problem.c to generate addition and subtraction math problems for a 4th grader....
Write a program named problem.c to generate addition and subtraction math problems for a 4th grader. 1. Your program will first ask user how many problems they want to do. 2. Repeat the following until you give user the number of problems that the user asks for.   a. Display the question number b. Generate a addition or subtraction problem using two random generated two digits (0-99). - The odd number of problems (1,3,5,…) will be addition problems. - The even...
Design and write a Python 3.8 program that gives and grades a math quiz. The quiz...
Design and write a Python 3.8 program that gives and grades a math quiz. The quiz will give 2 problems for each of the arithmetic operations: +, -, *, /, and %. First, two addition problems will be presented (one at a time) with random numbers between 1 and 20. Then two subtraction problems, multiplication, division and modulus. For division use // and do floor division; for example: for 10//3, the answer should be 3. For an example of the...
In PYTHON : as basic as possible Write a program that acts as a cash register....
In PYTHON : as basic as possible Write a program that acts as a cash register. The program will prompt how many items they are buying. Then they will input the price of each item (these should be decimal numbers). The program will display the SUBTOTAL, TAX ADDED (13%), and the TOTAL (with tax). Make sure you include $ signs and round to two decimal places ] Sample Output: How many items are you buying? 3 Enter in a price:...
Description: You are to develop a Java program that prints out the multiplication or addition table...
Description: You are to develop a Java program that prints out the multiplication or addition table given the users start and end range and type of table. This time several classes will be used. You are free to add more methods as you see fit – but all the methods listed below must be used in your solution. For the class Table the following methods are required: - Protected Constructor – stores the start and size of table, creates the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT