代码流是未预料到的

时间:2015-12-26 14:06:02

标签: python python-2.7 python-3.x

有人知道为什么这段代码打印出以下输出?

代码:

def main():
    try :
        while True :
            count = None
            average = None
            total = None
            text = raw_input("Enter a number \n")
            print "check 1"
            if text =="done" :
                print "check2"
                print "Total is :",total
                average = float(total)/count
                print "Average is :",average
                print "Count is :",count
                break
            print "check 3"
            count += 1
            print "check 4"
            total += text
            print "last check"
    except :
        print "Invalid"

main()

输出:

check 1
check 3
Invalid

1 个答案:

答案 0 :(得分:1)

正在打印,因为您的脚本中存在逻辑错误,并且您捕获了except处理程序中的所有错误并打印无效。

我可以告诉你错误的位置,但实际上你应该自己找到它。 删除裸的处理程序,然后程序本身会显示。