Question

In: Computer Science

PLEASE note. Numbers and signs are slightly askew but they are supposed to be the numbers...

PLEASE note. Numbers and signs are slightly askew but they are supposed to be the numbers and signs made up of rows and columns of stars and spaces. PYTHON coding help please

Part 6

Finally, put everything together and write a program that lets the user practice a series of random addition and subtraction problems. Begin by asking the user for a number of problems (only accept positive values) and a size for their numbers (only accept numbers between 5 and 10). The generate a series of random addition and subtraction problems - display the numbers to the user with your digital display functions. Then prompt the user for an answer and check the answer using your check_answer function. Your program should also keep track of how many correct questions the user answered during their game. Here's a sample run of the program, with user input bolded:

How many problems would you like to attempt? -5
Invalid number, try again

How many problems would you like to attempt? 5
How wide do you want your digits to be? 5-10: 3
Invalid width, try again

How wide do you want your digits to be? 5-10: 5

Here we go!

What is .....

*****
    *
*****
    *
*****

  *
  *
*****
  *
  *

    *
    *
    *
    *
    *

= 4
Correct!

What is .....

*****
    *
*****
*
*****



*****



*****
    *
    *
    *
    *

= -5
Correct!

What is .....

    *
    *
    *
    *
    *



*****



*****
*
*****
    *
*****

= 0
Sorry, that's not correct.

What is .....

*****
    *
*****
*
*****

  *
  *
*****
  *
  *

    *
    *
    *
    *
    *

= 3
Correct!

What is .....

*****
    *
*****
*
*****

  *
  *
*****
  *
  *

*****
    *
*****
*
*****

= 4
Correct!

You got 4 out of 5 correct!

Solutions

Expert Solution

Python Program

Addition and subtraction problem game. Here numbers and symbols are made up of rows and columns of stars and spaces.

import random  #random module used to generate integers

def displayNumber(num):  #displayNumber() is used to identify the number and call function corresponding to that number
    if num == 1:
        displayOne()
    elif num == 2:
        displayTwo()
    elif num == 3:
        displayThree()
    elif num == 4:
        displayFour()
    elif num == 5:
        displayFive()
    elif num == 6:
        displaySix()
    elif num == 7:
        displaySeven()
    elif num == 8:
        displayEight()
    elif num == 9:
        displayNine()

def displaySymbol(symbol):  #displaySymbol() is used to select plus or minus symbol. If passed parameter is 0, then plus symbol. Orelse minus symbol.
    if symbol == 0:
        displayPlus()
    else:
        displayMinus()
def displayOne():   #displays the number one in star pattern
    for i in range(5):
        print(" "*2,"*",)

def displayTwo():   #displays the number one in star pattern
    print("*"*5)
    print(" ","*")
    print("*"*5)
    print("*")
    print("*"*5)

def displayThree():   #displays the number one in star pattern
    print("*"*5)
    print(" ","*")
    print("*"*5)
    print(" ","*")
    print("*"*5)

def displayFour():   #displays the number one in star pattern
    print("*","*")
    print("*","*")
    print("*"*5)
    print(" ","*")
    print(" ","*")

def displayFive():   #displays the number one in star pattern
    print("*"*5)
    print("*")
    print("*"*5)
    print(" ","*")
    print("*"*5)

def displaySix():   #displays the number one in star pattern
    print("*"*5)
    print("*")
    print("*"*5)
    print("*"," ","*")
    print("*"*5)

def displaySeven():   #displays the number one in star pattern
    print("*"*5)
    for i in range(4):
        print(" ","*")

def displayEight():   #displays the number one in star pattern
    print("*"*5)
    print("*"," ","*")
    print("*"*5)
    print("*"," ","*")
    print("*"*5)

def displayNine():   #displays the number one in star pattern
    print("*"*5)
    print("*"," ","*")
    print("*"*5)
    print(" "*3,"*")
    print(" "*3,"*")

def displayPlus():   #displays the number one in star pattern
    print(" *")
    print(" *")
    print("*"*5)
    print(" *")
    print(" *")

def displayMinus():   #displays the number one in star pattern
    print()
    print()
    print("*"*5)
    print()
    print()

def check_answer(num1,num2,symbol):  #check_answer() used to check whether the user entered correct answer or not.
    if (symbol == 0 and num1+num2 == answer) or (symbol == 1 and num1-num2 == answer):
        return 1
    return 0

#Get how many attempts the user want to solve problem and store that count in timesAttempt variable
timesAttempt = int(input("How many problems would you like to attempt? "))
#If user entered the interger less than one, then again ask the user to enter number
while timesAttempt<=0:
    print("Invalid number, try again",end="\n\n")
    timesAttempt = int(input("How many problems would you like to attempt? "))
#Get number size between 5 and 10
numberSize = int(input("How wide do you want your digits to be? 5-10: "))
while numberSize<5 or numberSize>10:
    print("Invalid number, try again",end="\n\n")
    numberSize = int(input("How wide do you want your digits to be? 5-10: "))
#Start the problem solving game
print()
print("Here we go!")

correctAnswerCount = 0 
count = 0
while timesAttempt!=0:
    a = random.randint(1, numberSize) #Generate an integer between 1 and numSize(the value entered by user)
    b = random.randint(1, numberSize) #Generate an integer between 1 and numSize(the value entered by user)
    symbol = random.randint(0, 1)     #Generate an integer 0 or 1. Consider 1 means minus operation and 0 means plus operation
    print()
    print("What is .....",end="\n\n")
    displayNumber(a)      #Display number which is stored in a
    print()
    displaySymbol(symbol) #Display symbol based whether it is plus or minus
    print()
    displayNumber(b)      #Display number which is stored in b
    print()
    answer = int(input("="))  #Ask user to enter the answer
    if check_answer(a,b,symbol) == 1:  #check whether answer is correct by calling check_answer() and if its return 1, then answer is correct
        print("Correct!")
        correctAnswerCount += 1
    else:  #If answer not correct, then display to user "not correct"
        print("Sorry, that's not correct.")
    timesAttempt-=1
    count += 1

print()
print("You got",correctAnswerCount,"out of",count,"correct!") # Show the scoreboard of user

Python Program Image

OUTPUT IMAGES


Related Solutions

You have to build a long cylinder that will be compressed at an angle slightly askew...
You have to build a long cylinder that will be compressed at an angle slightly askew from the long axis of the cylinder. That squeezing force will cause a Z-axis pancake stack (like most of our printed cylinders are) to shear apart at printed layers. What modifications do you suggest to the print of this cylinder that can resist this compression force?​
You go to monitor a patient and note that the patient is slightly uncomfortable. She is...
You go to monitor a patient and note that the patient is slightly uncomfortable. She is on Pressure Assist Control with the following settings: inspiratory pressure of 15cm H2O, PEEP 5 cm H2O, FIo2 .35, and an inspiratory time of 1.2 seconds. Upon careful examination you note that there is diaphragmatic activity prior to exhalation near the end of the breath. Question 1: What is the primary problem or issue to be addressed? Question 2: What can you do with...
Each of the following sets of quantum numbers is supposed to specify an orbital. Choose the...
Each of the following sets of quantum numbers is supposed to specify an orbital. Choose the one set of quantum numbers that does not contain an error. n = 4, l = 3, ml =-4 n = 2, l = 2, ml =0 n = 3, l = 2, ml =-2 n = 2, l = 2, ml =+1
Q2: The following code is supposed to return the sum of the numbers between 1 and...
Q2: The following code is supposed to return the sum of the numbers between 1 and n inclusive, for positive n. An analysis of the code using our "Three Question" approach reveals that: int sum(int n){ if (n == 1)     return 1; else     return (n + sum(n - 1)); } Answer Choices: it passes on all three questions and is a valid algorithm.     it fails the smaller-caller question.     it fails the base-case question.     it fails...
#72: Note: On this problem, some of your answers might be slightly different from Moodle's because...
#72: Note: On this problem, some of your answers might be slightly different from Moodle's because of rounding error. The table below gives the gold medal times for every other Summer Olympics for the women's 100-meter freestyle (swimming). Year Time (seconds) 1912 82.2 1924 72.4 1932 66.8 1952 66.8 1960 61.2 1968 60.0 1976 55.65 1984 55.92 1992 54.64 2000 53.8 2008 53.1 a. Decide which variable should be the independent variable and which should be the dependent variable. Independent...
Question 24 Part A Each of the following sets of quantum numbers is supposed to specify...
Question 24 Part A Each of the following sets of quantum numbers is supposed to specify an orbital. Choose the one set of quantum numbers that does not contain an error. n = 3, l = 2, ml =+3 n = 4, l = 4, ml =0 n = 4, l = 0, ml =-1 n = 5, l = 3, ml =-3 n = 3, l = 1, ml = -2
Fill in the missing numbers from some slightly modified recent Financial Statements. If I list an...
Fill in the missing numbers from some slightly modified recent Financial Statements. If I list an account area, that account areas is correct. Deferred income taxes (current asset) 5, Total current liabilities 93, Total current assets 101, Deferred revenue (Current liability) 10, Long-term investments 4, Short-term investments 4, Total liabilities 218, Other current assets 3, Short-term borrowings 21, Total assets 318, Accounts payable 49, Gross margin 195, Preferred stock ($5 par) 12, Merchandise inventory 76, Deferred income taxes (Long term...
Hoover Company signs a four month promissory note for $ 270,000 on January​ 31, 2016. The...
Hoover Company signs a four month promissory note for $ 270,000 on January​ 31, 2016. The company is required to pay $ 67, 500 on the note each month. The first payment is on February​ 1, 2016, and the final payment is on May​ 1, 2016. How will this note be reported on the balance sheet at January​ 31, 2016?
19. Salmond signs a note for the purpose of lending credit to John. Salmond also makes...
19. Salmond signs a note for the purpose of lending credit to John. Salmond also makes his father Richard sign the note because of his shaky financial condition. This indicates that Richard has signed the note in the capacity of a(n): A. drawee. B. accommodation maker. C. drawee. D. indorser. 20. Jack received a check as payment for the work he did. He indorsed the check "without recourse." This has what legal effect? A. The drawer of the check is...
Bob signs a note promising to pay Marie $4000 in 8 years at 8%compounded monthly....
Bob signs a note promising to pay Marie $4000 in 8 years at 8% compounded monthly. Then, 148 days before the note is due, Marie sells the note to a bank which discounts the note based on a bank discount rate of 14%. How much did the bank pay Marie for the note?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT