So, I am having troubles with two things: keys being drawn on top of each other, and not sure how to write conditionals. I basically have to type numbers between 0 an 100, and based on the numbers it calculates the grades as letters (A, B, C, D or F) once I click the mouse. I don’t have much knowledge about conditionals because the textbook I have isn’t good at all. My program isn’t complete because just don’t know what else to do.
avg = " "
def Grade_Calculator(calculator):
global avg
fill(0, 0, 255)
textSize(25)
if 0<avg and avg<59:
text("F", 90, 100)
elif 60<avg and avg<69:
text("D", 90, 100)
elif 70<avg and avg<79:
text("C", 90, 100)
elif 80<avg and avg<89:
text("B", 90, 100)
elif 90<avg and avg<100:
text("A", 90, 100)
else:
avg = " "
def setup():
background(255)
fill(255)
size(300, 300)
rect(50, 100, 200, 100)
fill(255, 0, 0)
textSize(18)
text("Grade Calculator", 60, 120)
textSize(14)
fill(0)
text("Average:", 60, 160)
def draw():
global avg
textSize(14)
text(avg, 150, 160)
def keyPressed():
global avg
avg = key
def mouseClicked():
global avg
Grade_Calculator(avg)
I am supposed to have at 2 global variables: the current keys pressed and the grade to be drawn. And also a function that takes an average as a parameter. There should have conditionals that will calculate the correct grade and return it. I created the function with the conditionals, but the fact that I only have one variable and no returns in the conditional function is what I think is wrong in my program. If you try it you’ll notice that I calculates the grade, but the grade disappears (maybe because something is drawn on top of it?)
B/c grade_calculator() is invoked from within mouseClicked(), whatever is displayed will be “shown” for 1 frame only.
By default, draw() is called back at about 60 FPS.
So each frame lasts about 16.7 milliseconds!
Rather than drawing from within user input callbacks, you just record the action has happened in some variable, but later display them when draw() runs.
I managed to fix that. My program is almost finished. I am having issues now only with mouseClicked() function crashing the program after a grade is assigned. It needs to be one click to give an alphabetic grade, then the second click to reset the grades and start over. I believe I am supposed to use something like if keys_pressed != " " then clear whatever was drawn, but I am still trying to figure out how, but all my attempts failed so far. My program so far is:
keys_pressed = " "
def calculation_grade():
global keys_pressed
textSize(25)
fill(255, 0, 0)
if 0<= int(keys_pressed) <= 59:
text("F", 210, 160)
elif 60<= int(keys_pressed) <=69:
text("D", 210, 160)
elif 70<= int(keys_pressed) <=79:
text("C", 210, 160)
elif 80<= int(keys_pressed) <=89:
text("B", 210, 160)
elif 90<= int(keys_pressed):
text("A", 210, 160)
def setup():
#rect, grade calculator and average in setup because they only need to be drawn once program starts
fill(255)
size(300, 300)
background(255)
textSize(14)
rect(50, 100, 200, 100)
fill(255, 0, 0)
textSize(18)
text("Grade Calculator", 60, 120)
textSize(14)
fill(0)
text("Average:", 60, 160)
def draw():
"""draws the numeric value in variable keys_pressed"""
global keys_pressed
textSize(14)
fill(0)
text(keys_pressed, 150, 160)
def keyPressed():
"""numeric grade is drawn whenever key is pressed"""
global keys_pressed
keys_pressed = keys_pressed + key
def mouseClicked():
"""assigns alphabetic grade according to the numeric grade typed"""
global keys_pressed
calculation_grade()
if keys_pressed != " ":
keys_pressed = " "