In: Computer Science
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!
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