我有这个程序工作,但是当我验证它需要在每个数字之后而不是最后验证。我将如何在每个数字输入后检查它并仍然保留它们自己的功能。当我得到各种错误后,它会直接返回一个数字。
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(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):
score = (num1 + num2 + num3 + num4 + num5)
average_score = score / 5.0
return score, average_score
def determine_grade(score):
if score > 90:return '4.0'
elif score > 80:return '3.0'
elif score > 70:return '2.0'
elif score > 60:return '1.0'
return '0.0'
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()