[Tutor] run with default value if input not given
Saad Javed
sbjaved at gmail.com
Mon Oct 29 07:28:05 CET 2012
I've come up with this:
try:
sys.argv[1]
x = sys.argv[1]
main(x)
except IndexError:
main(x)
It works but seems hackish.
Saad
On Monday, October 29, 2012, Saad Javed wrote:
> 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/ba505f46/attachment.html>
More information about the Tutor
mailing list