[Tutor] run with default value if input not given

Saad Javed sbjaved at gmail.com
Mon Oct 29 07:06:14 CET 2012


Hi,

#!/usr/bin/env python

import sys

x = 'Saad is a boy'

def main(x):
a = []
b = x.split(' ')
for item in b:
a.append(item)
print a
if __name__ == '__main__':
x = sys.argv[1]
main(x)


How can I make this program run with the default value of x if I don't
specify an argument at the command line?
It should do this:

saad at saad:~$ python test.py "Mariam is a girl"
['Mariam', 'is', 'a', 'girl']

saad at saad:~$ python test.py
['Saad', 'is', 'a', 'boy']

But the simply running "test.py" gives:
Traceback (most recent call last):
  File "input_test.py", line 13, in <module>
    x = sys.argv[1]
IndexError: list index out of range


Saad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121029/279e9b02/attachment.html>


More information about the Tutor mailing list