Question

Instructions Basically, you will modify the mathq program to make it satisfy a different set of...

Instructions

Basically, you will modify the mathq program to make it satisfy a different set of requirements, and fix what is not working in it. Specifically, imagine that the client for whom we created the mathq program for would now like some changes. To be exact:

Part 1: They want it to provide addition and subtraction problems as well as multiplication and division.

  • They still want the choice to be random, as before, but now the problem is randomly multiplication, division, addition, or subtraction.
  • For the addition and multiplication problems: the range of both operands is still limited to single digit numbers,
  • For the division and subtraction problems: The first operand is based on the second operand and the solution, where those are still limited to single digit numbers.


So,

18 / 9 = ?
>

is allowed, not  9 / 18 = ? .


Hence, the answers to the division and subtraction problems generated should be positive, single digit numbers in most cases.

So,

2 - 1 = ?
>

is allowed, not  1 – 2 = ? .

Part 2: Concerning division problems: The divide-by-zero problem should be fixed. Although zero can be an operand in multiplication (the first or the second), it cannot be the divisor (the second operand) in a division problem. 
So,

0 / 1 = ?
>


is allowed, but not  1 / 0 = ? .

Part 3: Concerning subtraction problems: The subtrahend is the number that is subtracted from the minuend. The problems cannot have value of zero as the minuend (the first operand), although it can be the subtrahend (the second operand).

So,

6 – 0 = ?

>

is allowed, but not  0 - 6 = ? .

The only situation where zero is permitted to be the minuend is it if is subtracted from itself. That is,

0 – 0 = ?
>

is the only circumstance of zero minuend allowed.

Part 4: The user should be able to type either the letter character 'q' or 'Q' to quit the mathq program.

Part 5: When the program starts, in addition to displaying a "welcome message", it should 
display a message to the user about how to quit the program anytime.

Part 6: Reporting a score would likely motivate the user to continue playing the game. The program should display the user's score after they quit the mathq program.

  • Each time the user answers a question correct, they earn 1 score point.
  • An incorrect answer is worth 0 points.
  • Only correct and incorrect answers are counted, not invalid responses.

When the user quits, the program should display the total number of correct and incorrect responses and the percentage correct. For example, if the user answered 12 questions correctly and 6 questions incorrectly, the program would display something like:

You answered 12 out of 18 questions correctly, or 67% correctly.

Thank you for playing mathq.

The score is announced only when the user answers questions. When the user quits the mathq program immediately without properly playing the game, the score should not be announced, since they have not answered any questions in the appropriate manner to earn a score.

The expression of the user’s score of correct answers as a percentage is not required for the score presentation.

All programs must be thoroughly tested before they are released to users. How should you test the program? This is an interactive program, so it is not a matter of creating many files and running the program on them. You have to pretend you are a user and play many games with it, giving it invalid responses, valid responses, right answers, wrong answers, and so on. Make sure that it counts the correct answers properly, and incorrect ones. What happens if the user quits without playing at all? Make sure it works as intended for that situation as well.

We are using Linux bash and python

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import random
correct=0
wrong=0
flag=0
def add():
        global correct,wrong,flag
        i = random.randint(0,9)
        j = random.randint(0,9)
        print("%s + %s is: "%(i,j))
        ans = input()
        if ans=='q' or ans=='Q':
                flag = 1
                return
        for x in ans:
                if x<'0' or x>'9':
                        print("Invalid input! Skipping to next question.")
                        return
        ans = int(ans)
        if ans==i+j:
                print("Correct answer!")
                correct += 1
        else:
                print("Wrong answer!")
                wrong+=1

def sub():
        global correct,wrong,flag
        i = random.randint(0,9)
        j = random.randint(0,i)
        print("%s - %s is: "%(i,j))
        ans = input()
        if ans=='q' or ans=='Q':
                flag = 1
                return
        for x in ans:
                if x<'0' or x>'9':
                        print("Invalid input! Skipping to next question.")
                        return
        ans = int(ans)
        if ans==i-j:
                print("Correct answer!")
                correct+=1
        else:
                print("Wrong answer!")
                wrong+=1

def mul():
        global correct,wrong,flag
        i = random.randint(0,9)
        j = random.randint(0,9)
        print("%s * %s is: "%(i,j))
        ans = input()
        if ans=='q' or ans=='Q':
                flag = 1
                return
        for x in ans:
                if x<'0' or x>'9':
                        print("Invalid input! Skipping to next question.")
                        return
        ans = int(ans)
        if ans==i*j:
                print("Correct answer!")
                correct+=1
        else:
                print("Wrong answer!")
                wrong+=1

def div():
        global correct,wrong,flag
        i = 0
        j = 0
        while True:
                i = random.randint(0,9)
                j = random.randint(1,9)
                if i%j==0:
                        break
        print("%s / %s is: "%(i,j))
        ans = input()
        if ans=='q' or ans=='Q':
                flag = 1
                return
        for x in ans:
                if x<'0' or x>'9':
                        print("Invalid input! Skipping to next question.")
                        return
        ans = int(ans)
        if ans==i/j:
                print("Correct answer!")
                correct+=1
        else:
                print("Wrong answer!")
                wrong+=1
print("Enter q to Quit the program any time: ")

while flag==0:
        x = random.randint(0,3)
        if x==0:
                add()
        elif x==1:
                sub()
        elif x==2:
                mul()
        else:
                div()
        if flag==0:
                print("Correct response "+str(correct))
                print("Incorrect response "+str(wrong))
if correct==0 and wrong==0:
        print("You did not play!")
else:
        print("You answered "+str(correct)+ " out of "+str(correct+wrong) +" questions correctly, or "+str(round(((correct)/(correct+wrong))*100))+"% correctly.\nThank you for playing mathq.")


Sample output:

kishan@ubuntu: -/WORK a kishan@ubuntu: -/WORK$ python3 HomeworkLib.py Enter q to quit the program any time: 3 * 9is: 27 Correct ans

Add a comment
Know the answer?
Add Answer to:
Instructions Basically, you will modify the mathq program to make it satisfy a different set of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This week we looked at an example math program that would display the answer to a...

    This week we looked at an example math program that would display the answer to a random math problem. Enhance this assignment to prompt the user to enter the solution to the displayed problem. After the user has entered their answer, the program should display a message indicating if the answer is correct or incorrect. If the answer is incorrect, it should display the correct answer. The division section should use Real data types instead of Integer data types. Keep...

  • Write a MIPS math quiz program in MARS. The program should start with a friendly user...

    Write a MIPS math quiz program in MARS. The program should start with a friendly user greeting. From there, it should generate a random arithmetic problem. Your program will need to generate three random things: the first and second operand and the operator to use. Your program should generate random positive integers no greater than 20 for the operands. The possible operators are +, -, * and / (division). The user should be prompted for an answer to the problem....

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Read description carefully. Needs to catch exceptions and still be able to keep the program going...

    Read description carefully. Needs to catch exceptions and still be able to keep the program going on past the error. Program should allow user to keep going until he or she quits Math tutor) Write a program that displays a menu as shown in the sample run. You can enter 1, 2. 3, or 4 for choosing an addition. subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter...

  • Please put the outputs with it also I want to make sure it runs thanks Project-Computer-Assisted...

    Please put the outputs with it also I want to make sure it runs thanks Project-Computer-Assisted Instruction (50 points) use of computers in education is referred to as computer-assisted instruction (CAl) should then Writ e a program that will help an elementary school student learn multiplication. Use the rand function to produce two positive one-digit integers. The program prompt the user with a question, such as How much is 6 times 7? The student then inputs the answer. Next, the...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Integer Math- Console based---uses Windows Form in Visual Basic   Create an application that uses random integers...

    Integer Math- Console based---uses Windows Form in Visual Basic   Create an application that uses random integers to test the user’s knowledge of arithmetic. Let the user choose from addition, subtraction, multiplication, and division. The integers used in the problems should range from 20 to 120. When giving feedback, use color to differentiate between a correct answer response, versus an incorrect answer response. Also check for non-integer input. Preparing division problems requires special consideration because the quotient must be an integer....

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the...

    (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the four basic arithmetic operations, i.e., addition, subtraction, multiplication and integer division. A sample partial output of the math quiz program is shown below. The user can select the type of math operations that he/she would like to proceed with. Once a choice (i.e., menu option index) is entered, the program generates a question and asks the user for an answer. A sample partial output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT