NameError: name 'host' is not defined

后端 未结 3 1859
猫巷女王i
猫巷女王i 2021-01-28 17:23

I\'m new in python programming. When i try running a simple python script i get error like this in my terminal

root@bt:/tmp# python code.py
Traceback (most recen         


        
3条回答
  •  旧巷少年郎
    2021-01-28 18:04

    Your except clause will catch any error in any line of code in the try block. If you don't specify enough arguments on the command line, the line host = str(sys.argv[1]) will fail, leaving host unassigned, which then causes the error you are seeing when you try to print it.

    You should take most of the code out of your try block, really, and/or create multiple try blocks that catch errors in much smaller chunks of code. Furthermore, you should specify the actual exception type you want to handle with each except instead of trying to handle all of them. Bare except: catches things you probably don't want caught, such as KeyboardInterrupt and SystemExit. If you must catch most exceptions, use except Exception: instead of just except:.

提交回复
热议问题