Python基本循环问题

时间:2018-03-09 21:50:20

标签: python-3.x loops

我试图让下面的代码在Python中运行,并且我在返回点n继续收到错误。有人可以看看代码,看看我搞砸了哪里?

while True:
    n = input(prompt)
    try:
        n = float(n)
        if n < n1 or n > n2:
            print('Error. Input was out of range')
        else:
            break
    except:
        print('Error. Please enter a number')
return n

def main():
    average = 0
    count = 0
    while(True):
        name = input("Please enter a student name or '*' to finish: ")
    if name == '*':
        break
        scores = 0
        scores += getPosFloat('Please enter a score for ' + name + ': ', 0, 100)
        scores += getPosFloat('Please enter another score for ' + name + ': ', 0, 100)
        scores += getPosFloat('Please enter another score for ' + name + ': ', 0, 100)
    print('The average score for ' + name+ ' is: %.2f' % (scores/3))
    #please give the proper argument
    average += getPosFloat('Please enter average score for ' + name + ': ', 0, 100)
    count += 1
    if count!=0:
        print("The class average is: ",(average)/count)
main()

2 个答案:

答案 0 :(得分:0)

您无法在函数外返回值。 (我假设这就是你提供的全部代码)

答案 1 :(得分:0)

您缺少功能定义。看起来你应该

main()

$Literal之前还有其他错误,但这会让你开始