Re: [Tutor] run with default value if input not given

2012-10-29 Thread Oscar Benjamin
On 29 October 2012 13:32, Oscar Benjamin wrote: > > def main(x, y='default'): > print(x) > print(y) > > if __name__ == "__main__": > main(sys.argv[1:]) A quick correction. That should be (note the *): if __name__ == "__main__": main(*sys.argv[1:]) Oscar

Re: [Tutor] run with default value if input not given

2012-10-29 Thread Oscar Benjamin
On 29 October 2012 06:06, 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

Re: [Tutor] run with default value if input not given

2012-10-29 Thread Brian van den Broek
On 29 Oct 2012 02:30, "Saad Javed" wrote: > > 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 Saad, The first sys.argv is not needed. Notice how i have replied below the text i am quoting? That is

Re: [Tutor] run with default value if input not given

2012-10-29 Thread Peter Otten
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 va

Re: [Tutor] run with default value if input not given

2012-10-29 Thread Steven D'Aprano
On Mon, Oct 29, 2012 at 11:28:05AM +0500, Saad Javed wrote: > 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. There's no need to look up sys.argv[1] twice, nor any need to write main(x) in both blocks. You

Re: [Tutor] run with default value if input not given

2012-10-29 Thread Steven D'Aprano
Saad, please don't send HTML emails, as it destroys the essential formatting of your code. On Mon, Oct 29, 2012 at 11:06:14AM +0500, Saad Javed wrote: > How can I make this program run with the default value of x if I don't > specify an argument at the command line? arguments = sys.argv[1:] #

Re: [Tutor] run with default value if input not given

2012-10-28 Thread Saad Javed
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

[Tutor] run with default value if input not given

2012-10-28 Thread Saad Javed
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