First things first, I think the code you originally posted (with Hello(sys.argv[0])
) is not what you actually have. It doesn’t match the error, which states sys.argv[1]
, so what you probably have is:
def main():
Hello(sys.argv[1])
As to the error then, it’s because you haven’t provided an argument when running. You need to do so, such that sys.argv[1]
exists:
python helloprog Pax
You would find a more robust main
as:
def main():
if len(sys.argv) < 2:
Hello("whoever you are")
else:
Hello(sys.argv[1])
which will detect when you haven’t provided an argument, and use a suitable default rather than raising an exception.
1
solved What is the error in following python code