简体   繁体   English

python 中的回溯是什么?

[英]What is traceback in python?

My code我的代码

largest = None
smallest = None

while True:
    num = input("Enter a number: ")
    if num == "done" : break

    try:
        fnum=int(num)


    except:
            print("Invalid input")

    if largest is None:
        largest=fnum

    elif fnum > largest:
          largest=fnum

    if smallest is None:
        smallest=fnum
    elif fnum<smallest:
            smallest=fnum


print("Maximum is", largest)
print("Minimum is", smallest)

The Traceback error:回溯错误:

Traceback (most recent call last): File "C:\Users\Marjia Priyaa\Desktop\Python\maximum.py", line 16, in largest=fnum NameError: name 'fnum' is not defined回溯(最后一次调用):文件“C:\Users\Marjia Priyaa\Desktop\Python\maximum.py”,第 16 行,最大 = fnum NameError: name 'fnum' is not defined

Python's traceback is the order of things going wrong in your code. Python 的回溯是代码中出错的顺序。 Typically the last line is what's relevant to your error.通常最后一行是与您的错误相关的内容。 In this case you are trying to reference a variable fnum which is considered undeclared because it is out of scope (trying to use non-existent variable).在这种情况下,您尝试引用一个变量 fnum,该变量被认为是未声明的,因为它超出了 scope(尝试使用不存在的变量)。 Try setting fnum = 0 before the try statement.尝试在 try 语句之前设置 fnum = 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM