我有这个程序正常工作但是当我验证它需要在每个数字之后验证而不是在最后。如何在每次输入数字后进行检查,并依次保留其功能。当我遇到各种各样的错误后,我会直接返回一个数字。
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()
答案 0 :(得分:1)
有一个要求&amp;的功能读取一个数字,直到输入一个有效数字,然后返回该有效数字。为5个输入中的每个输入调用它。 (您可能希望传递此函数所获得的数字。)
答案 1 :(得分:0)
这应该做你想要的。正如斯科特建议的那样,最大的关键区别在于采用通用方法来收集回复。
在此解决方案中,getinput()
收集并验证单个响应,与所有其他集合隔离。然后根据main函数的要求多次调用它,并将经过验证的数据项放入scores
数据结构中以供以后处理。
下一个关键区别是我们已将所有处理细节抽象为calprint
,然后在必要时调用calc_average
和determine_grade
,而不是必须传递原始7个参数进入函数。
def main():
scores = []
for i in range(5):
x = getinput(i)
scores.append(x)
calprint(scores)
def getinput(i):
ordinal = { 1:'first',
2:'second',
3:'third',
4:'forth',
5:'fifth'}
o = ordinal.get(i+1,"next")
x = int(input('Please enter your '+o+' test score: '))
while not(0 < x < 100):
print ('Error--- The number musy be at least 0 and not more than 100.')
x = int(input('Please enter your '+o+' test score: '))
return x
def calc_average(scores):
return (sum(scores)+0.0)/len(scores)
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 (scores):
print
for i,s in enumerate(scores):
print ("Score #%d %s"%(i+1,format (s)))
print
print ("Average score ",format (calc_average(scores)))
print ("Average grade ",format (determine_grade(calc_average(scores))))
main()