Reputation: 15
My program works fine until i added the verfiy_num function where am i messing up? After i added the function to validate after each number it errors out. I need it to verify after each number entry and i cant seem to get it to work.
def main():
num1, num2, num3, num4, num5 = getinput()
num1, num2, num3, num4, num5 = verify_num(num1, num2, num3, num4, num5)
average_score,score = calc_average(num1, num2, num3, num4, num5)
average_score = determine_grade(average_score)
calprint(num1, num2, num3, num4, num5, score, average_score)
def getinput():
num1 = int(input('Please enter your first test score: '))
num2 = int(input('Please enter your second test score: '))
num3 = int(input('Please enter your third test score: '))
num4 = int(input('Please enter your fourth test score: '))
num5 = int(input('Please enter your fifth test score: '))
return num1, num2, num3, num4, num5
def verify_num(num1, num2, num3, num4, num5):
while num1 < 0 or num1 > 100:
print ('Error--- The number musy be at least 0 and not more than 100.')
num1 = int(input('Please enter your first test score: '))
while num2 < 0 or num2 > 100:
print ('Error--- The number musy be at least 0 and not more than 100.')
num2 = int(input('Please enter your second test score: '))
while num3 < 0 or num3 > 100:
print ('Error--- The number musy be at least 0 and not more than 100.')
num3 = int(input('Please enter your third test score: '))
while num4 < 0 or num4 > 100:
print ('Error--- The number musy be at least 0 and not more than 100.')
num4 = int(input('Please enter your fourth test score: '))
while num5 < 0 or num5 > 100:
print ('Error--- The number musy be at least 0 and not more than 100.')
num5 = int(input('Please enter your fifth test score: '))
return num1, num2, num3, num4, num5
def calc_average(num1, num2, num3, num4, num5):
average_score = (num1 + num2 + num3 + num4 + num5) / 5
score = (num1 + num2 + num3 + num4 + num5) / 5
return score, average_score
def determine_grade(average_score):
if average_score > 89: return 'A'
elif average_score > 79: return 'B'
elif average_score > 69: return 'C'
elif average_score > 59: return 'D'
return 'F'
def calprint (num1, num2, num3, num4, num5, score, average_score):
print
print ("Score #1 ", format (num1))
print ("Score #2 ", format (num2))
print ("Score #3 ", format (num3))
print ("Score #4 ", format (num4))
print ("Score #5 ", format (num5))
print ()
print ("Average score",format (score))
print ("Average grade",format (average_score))
main()
Upvotes: 0
Views: 105
Reputation: 47058
it's because your return statement is tabbed inside the last while loop. There is the chance that it never enters the last while loop, so just untab your return statement by one.
Also, why not just verify while getting the input?
words = ['first', 'second', 'third', 'fourth', 'fifth']
dialogs = ["Please enter your {0} test score: ".format(word) for word in words]
def getinput(i)
num = int(input(dialogs[i]))
while num < 0 or num > 100:
print ('Error--- The number musy be at least 0 and not more than 100.')
num = int(input(dialogs[i]))
return num
Upvotes: 1