In I need help with part E, 5, and 6 in python ! Write the program prolog as com
ID: 3866245 • Letter: I
Question
In I need help with part E, 5, and 6 in python !
Write the program prolog as comments in the first few lines of your program. (Use plain English, not python code and use # at the beginning of the program to write the comments.) The program prolog includes your name, date and time you finished working on the program and a description of your program.
Inside your program write the following design as comments. After the # symbol, give every step of your design a number so you can refer to it later on.
This is a suggested design for the program.
1. Inside the main function, create a graphics window 600*600.
2. Call a function DisplayWindow and pass it to one argument which is the name of the window you created in step 1 that will make the window background to be blue.
3. Then inside the main use for loop to ask the user for 15 questions. 4. Inside the for loop do the following:
A. Call a function GenerateNumber that takes no parameters and returns one value which is a random number between 0 and 30. Store the returned value inside a variable called number1.
B. Call a function GenerateNumber again and store the returned value inside a variable called number2.
C. Call a function GenerateOperation that takes no parameters and returns one value which is a random number between 1 and 4 (1 and 4 are included). Store the returned value inside a variable called operationType.
D. Call a function VerifyOperation that takes three parameters and returns two values. The passed arguments are number1, number2 and operationType. And the function returns two values (The first one is a Boolean value –either True or False- and the second one is numerical value – either -1 or the actual result of the operation–. Store the returned values into two variables under the name ValidOrNot and ActualResult.
E. If the value returned from 4.D is false, then you have to write a while loop and repeat steps 4.A 4.B and 4.C
F. Otherwise, you have to call function DisplayProblem that takes six parameters (The passed arguments are the window name, number1, number2 and operationType, ActualResult, and the current problem number) and it returns a Boolean value. Used the returned value to increase the counter of the value of a variable called CorrectAnswer by one. CorrectAnswer should be given the value zero before the for loop, CorrectAnswer is a counter that calculates the number of correct answers
5. After you finish showing and calculating the 15 problems call a function
DisplayStatistic and pass two arguments. The first one is the window name and the second one is the CorrectAnswer variable.
6. The program then waits for a click before terminating the game.
Explanation / Answer
main.py
from graphics import *
from random import *
from time import *
def CreateWindow(): #function creates game window with blue background and size 600x600
window = GraphWin("Math Program",600,600)
window.setBackground(color_rgb(96,196,230))
return window
def GenerateNumber(): #generates a random number 1-30
return randrange(1,31)
def GenerateOperation(): #generates a random number 1-4 (used to signify +, -, *, /)
return randrange(1,5)
def VerifyOperation(num1,num2,operation): #verifies that a math problem is valid for the goals of the game
if operation == 1:
answer = num1 + num2
elif operation == 2:
answer = num1 - num2
elif operation == 3:
answer = num1 * num2
elif operation == 4:
answer = num1 / num2
if answer < 0 or answer > 100 or answer % 1 != 0: #ensure the problem gives an integer response between 1 and 100
return False
else:
return True
def DisplayProblem(num1, num2, operation, window): #displays a problem on the screen
#text box aesthetics
T1 = Text( Point(200, 200), str(num1))
T1.setSize(35)
T2 = Text( Point(330, 200), (str(num2)+" = "))
T2.setSize(35)
T1.draw(window)
T2.draw(window)
#display operation based on "operation" random int (1 for +, 2 for -. etc.)
if operation == 1:
T3 = Text(Point(250, 200), "+")
rightanswer = num1+num2
elif operation == 2:
T3 = Text(Point(250, 200), "-")
rightanswer = num1-num2
elif operation == 3:
T3 = Text(Point(250, 200), "*")
rightanswer = num1*num2
elif operation == 4:
T3 = Text(Point(250, 200), "/")
rightanswer = num1/num2
T3.setSize(35)
T3.draw(window)
#entry box for user to enter an answer
entry1 = Entry(Point(400,200), 2)
entry1.setSize(30)
entry1.draw(window)
window.getMouse()
answer = entry1.getText()
#undraw entry screen when user gives an answer
T1.undraw()
T2.undraw()
T3.undraw()
entry1.undraw()
#create a circle after an answer is given
circle1 = Circle( (Point(300,350)), 150)
#if the answer is right, fill circle with green and display "Correct!"
if answer == str(int(rightanswer)):
circle1.setFill("green")
circle1.draw(window)
T6 = Text(Point(300, 350), "Correct!")
T6.setSize(30)
T6.draw(window)
window.getMouse()
circle1.undraw()
T6.undraw()
return 1
#if answer is wrong, fill circle with red and display "Wrong answer!"
else:
circle1.setFill("red")
circle1.draw(window)
T7 = Text(Point(300, 350), "Wrong Answer!")
T7.setSize(30)
T7.draw(window)
T8 = Text(Point(300,395), ("The correct answer was "+str(int(rightanswer))))
T8.draw(window)
window.getMouse()
circle1.undraw()
T7.undraw()
T8.undraw()
return 0
def DisplayStatistic(correct, window):
#function displays the results to the user: how many questions right out of 15
percent = 100 * (correct/15)
percent = int(percent)
resultText = Text( (Point(300,300)), ("You got "+str(correct)+" problems correct out of 15!"))
resultText.setSize(20)
percentText = Text( (Point(300,350)), ("That's "+str(percent)+" percent!"))
percentText.setSize(15)
percentText.draw(window)
resultText.draw(window)
def DisplayWindow(window):
#function for game window
T4 = Text( Point(300, 50), "Answer the problems!")
T4.setSize(35)
T4.draw(window)
T5 = Text( Point(300, 100), "Click to continue.")
T5.setSize(20)
T5.draw(window)
correct = 0
for i in range(15):
#ask 15 problems
T9 = Text( Point(100, 550), ("Problem "+str(i+1)) )
T9.setSize(20)
T9.draw(window)
condition = False
operationType = GenerateOperation() #generate operations and random numbers
while condition == False:
number1 = GenerateNumber()
number2 = GenerateNumber()
if VerifyOperation(number1, number2, operationType) == True: #verify that problem is valid (integer answers between 1-100)
condition = True
correct += DisplayProblem(number1, number2, operationType, window) #increment num of correct answers
T9.undraw() #undraw previous question
T4.undraw()
T5.undraw()
DisplayStatistic(correct, window) #after 15 questions, display statistics
def main(): #main method
window = CreateWindow() #create game window
DisplayWindow(window) #display game window
window.getMouse() #get mouse click to close program on end
window.close()
main() #call main method
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.