I am attempting to turn my code into def functions for an assignment. I have defined the functions and I have called to them in the main function. However, the code does not function. The error displayed is Traceback (most recent call last): File "C:\Users\iamep\Desktop\Python Projects\TestAvgCalc2-1.py", line 31, in average = total / total_quiz ZeroDivisionError: division by zero
I believe I need to assign global variables. How would I do this?
Here is the Code:
#Welcome Message
print("Welcome to the Test Average Calculator!")
print("Say 'stop' when you are done with Data Entry")
#Variables
total = 0
total_quiz = 0
def testAvgCalculation():
while True:
#User Input and Variable to stop loop
inpt = input("Enter score: ")
if inpt.lower()== 'stop':
break
#Data Validation
try:
if int(inpt) in range(1,101):
total += int(inpt)
total_quiz += 1
else:
print("Score too small or Big")
except ValueError:
print("Not a Number")
#Average Calculation
average = total / total_quiz
def displayAverage():
#Display Average and amount of test scores input
print('The Average score is: ', format(average, '.2f'))
print('You have entered', total_quiz, 'scores')
#Main Function
def main():
testAvgCalculation()
displayAverage()
#Run Main Function
main()