1

我有这个程序工作,但是当我验证它需要在每个数字之后而不是最后验证。我将如何在每个数字输入后检查它并仍然保留它们自己的功能。当我得到各种错误后,它会直接返回一个数字。

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()
4

2 回答 2

1

有一个函数要求并读取一个数字,直到输入一个有效数字,然后返回该有效数字。为您的 5 个输入中的每一个调用它。(您可能希望将此函数传递给它正在获取的数字。)

于 2013-11-10T23:57:58.300 回答
0

这应该做你想要的。正如 Scott 所建议的,最大的关键区别在于采用通用方法来收集响应。

在此解决方案中,getinput()收集和验证单个响应,与所有其他集合隔离。然后根据 main 函数的要求多次调用它,并将经过验证的数据项放入scores数据结构中以供以后处理。

下一个关键区别是我们将所有处理细节抽象到calprint中,然后在必要时调用calc_averageand 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()
于 2013-11-11T00:09:14.023 回答