import sys
def Hello(name):
name = name + '!!!'
print 'Hello' , name
def main():
Hello(sys.argv[1])
if __name__ == '__main__':
main()
Here is the error
Traceback (most recent call last):
File "D:\pythonPractice\firstPython.py", line 13, in <module>
main()
File "D:\pythonPractice\firstPython.py", line 9, in main
Hello(sys.argv[1])
IndexError: list index out of range
I have also tried sys.argv[2] but error remains
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 statessys.argv[1], so what you probably have is: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:You would find a more robust
mainas:which will detect when you haven’t provided an argument, and use a suitable default rather than raising an exception.