0

我的程序运行良好,直到我添加了 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()
4

1 回答 1

1

这是因为您的 return 语句在最后一个 while 循环中被标记。它有可能永远不会进入最后一个 while 循环,所以只需将你的 return 语句取消一个。

另外,为什么不只是在获取输入时进行验证?

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
于 2013-11-10T18:07:37.223 回答