我的程序运行良好,直到我添加了 verfiy_num 函数我在哪里搞砸了?在我添加功能以在每个数字之后进行验证后,它会出错。我需要它在每次输入数字后进行验证,但我似乎无法让它工作。
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()