Using python code, while completing the grade breakdown of a class, I must sum a
ID: 3852345 • Letter: U
Question
Using python code, while completing the grade breakdown of a class, I must sum all weighted scores I received in midterm 1, midterm 2, final, and homework. All values are created in their own respective functions. I believe I have to return these values into my overall_grade function but cannot find the correct sytax. Listed directly below is an example of what should be printed out. Below the example is my progress in the code. As previously mentions, the functions midterm_1():, midterm_2():, final():, and homework(): create values I must use in the function overall_grade. I will then call all of these function in the main function.
To clarify, I need help finding the correct syntax to properly return variables used in previous functions in a new function to calculate the overall grade.
### my code
def midterm_1():
print("Midterm 1:")
#Enter the weight of Midterm 1
weight_1 = int(input("Weight (0-100)? "))
#Enter the score on Midterm 1
score_earned = int(input("Score earned? "))
#If score was shifted, press 1. If score was NOT shifted press 2.
score_shift = int(input("Were scores shifted (1=yes, 2=no)? "))
if (score_shift == 1):
shift_amount = int(input("Shift amount? "))
score_earned = shift_amount + score_earned
#If a score higher of 100 is entered, set the score to 100
if (score_earned > 100):
score_earned = 100
#Display points earned as a fraction over 100
total_points = print("Total points = {} / 100" .format (score_earned))
#Display the score earned after being adjusted by the weight
weighted_score_midterm1 = float((score_earned/100)*weight_1)
weighted_score_midterm1_print = print("Weighted score = {} / {} " .format (weighted_score_midterm1, weight_1))
def midterm_2():
print("Midterm 2:")
#Enter the weight of Midterm 1
weight_2 = int(input("Weight (0-100)? "))
#Enter the score on Midterm 1
score_earned = int(input("Score earned? "))
#If score was shifted, press 1. If score was NOT shifted press 2.
score_shift = int(input("Were scores shifted (1=yes, 2=no)? "))
if (score_shift == 1):
shift_amount = int(input("Shift amount? "))
score_earned = shift_amount + score_earned
#If a score higher of 100 is entered, set the score to 100
if (score_earned > 100):
score_earned = 100
#Display points earned as a fraction over 100
total_points = print("Total points = {} / 100" .format (score_earned))
#Display the score earned after being adjusted by the weight
weighted_score_midterm2 = float((score_earned/100)*weight_1)
weighted_score_midterm2_print = print("Weighted score = {} / {} " .format (weighted_score_midterm2, weight_2))
def final():
print("Final:")
weight_final = int(input("Weight (0-100)? "))
score_earned = int(input("Score earned? "))
score_shift = int(input("Were scores shifted (1=yes, 2=no)? "))
if (score_shift == 1):
shift_amount = int(input("Shift amount? "))
score_earned = shift_amount + score_earned
if (score_earned > 100):
score_earned = 100
total_points = print("Total points = {} / 100" .format (score_earned))
weighted_score_final_calc = float((score_earned/100)*weight_final)
weighted_score_final_print = print("Weighted score = {} / {} " .format (weighted_score_final_calc, weight_final))
def homework():
print("Homework:")
weight_homework = int(input("Weight (0-100)?" ))
num_assignments = int(input("Number of assignments? "))
index = 0
totalAssignScore = 0
totalAssignScoreMax = 0
while index < num_assignments:
print("Assignment", index+1, " score?", end='')
assignScore = int(input());
print("Assignment", index+1, " max?", end='')
assignScoreMax = int(input());
index = index + 1
totalAssignScore = totalAssignScore + assignScore
totalAssignScoreMax = totalAssignScoreMax + assignScoreMax
section_att = int(input("How many sections did you attend? "))
section_points = section_att * 3
if (section_points > 34):
section_points = 34
max_Section_Points = 34
print("Section points = {} / {}" .format(section_points, max_Section_Points))
totalPointsScored = section_points + totalAssignScore
totalPointsPossible = totalAssignScoreMax + max_Section_Points
print("Total points = {} / {}" .format(totalPointsScored, totalPointsPossible))
weighted_score_homework_calc = float(((totalPointsScored/totalPointsPossible))*weight_homework)
weighted_score_homework_print = print("Weighted score = {} / {} " .format (weighted_score_homework_calc, weight_homework))
def overall_grade():
#This function will use values given from midterm, final, and homeowork function
#to calculate total percentage and assign a letter grade.
# Grade = weighted midterm 1 + weighted midterm 2 + weighted final + weighted homework
def main():
midterm_1()
midterm_2()
final()
homework()
overall_grade()
main()
Explanation / Answer
As per your request, I'm giving you the way to get values from the four functions.
def midterm_1():
print("Midterm 1:")
#Enter the weight of Midterm 1
weight_1 = int(input("Weight (0-100)? "))
#Enter the score on Midterm 1
score_earned = int(input("Score earned? "))
#If score was shifted, press 1. If score was NOT shifted press 2.
score_shift = int(input("Were scores shifted (1=yes, 2=no)? "))
if (score_shift == 1):
shift_amount = int(input("Shift amount? "))
score_earned = shift_amount + score_earned
#If a score higher of 100 is entered, set the score to 100
if (score_earned > 100):
score_earned = 100
#Display points earned as a fraction over 100
total_points = print("Total points = {} / 100" .format (score_earned))
return score_earned
def overall_grade():
# This function will use values given from midterm, final, and homework function
# to calculate total percentage and assign a letter grade.
# Grade = weighted midterm 1 + weighted midterm 2 + weighted final + weighted homework
# get value of midterms
midterm1_value = midterm_1()
midterm2_value = midterm_2()
final_value = final()
homework_value = homework()
# now you can use the values here.
# remember to add the return statement to allt he four functions.
-------------------------
Notice the last line that I added to the def midterm_1() function. You have to add such line to each of your four functions.
Another alternative (which I feel is more elegant) is the following:
You return the values to main() using the same method as I mentioned above.
Then you print the required scores inside main().
Then you call the overall_grade() by passing it all the values from main itself.
And again print the result in main().
This seems more elegant.
But to do it this way, you need to learn how to send the values. (You already know how to return values).
Sending values can be done in following way:
function2(y):
# here y has the value 5 when function2 is called by function1
# do something with y
return something
# the returned value goes to 'result' variable in function1()
function1():
x = 5;
result = function2(5)
# do somethign with result
The above should clear up how to send values and return values. If you still have any queries or do not understand something, ask in comments. I will help you with it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.