NameError: name 'host' is not defined

后端 未结 3 1858
猫巷女王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:.

    0 讨论(0)
  • 2021-01-28 18:09

    you are defining host in the first line of try/except
    i believe the error is in that first line.
    to debug this take remove the try/except to see what the actual error is.

    0 讨论(0)
  • 2021-01-28 18:13

    it seem that your script expects an input parameter

    host=str(sys.argv[1])

    in case that parameter is not supplied, as shown in your post, an exception raised and been caught in the except clause before the host parameter was defined

    try to declare host before the try/except block

    0 讨论(0)
提交回复
热议问题