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@saad:~$ python test.py "Mariam is a girl"
> ['Mariam', 'is', 'a', 'girl']
>
> saad@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
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to