[Tutor] Sys.argv read parameters

Alan Gauld alan.gauld at btinternet.com
Thu Apr 18 02:12:08 CEST 2013


On 17/04/13 20:27, Danilo Chilene wrote:

> import sys
>
> a = 'This is A'
> b = 'This is B'
> c = 'This is C'
>
> for i in sys.argv[1]:
>      if sys.argv[1] == 'a':
>          print a
>      if sys.argv[1] == 'b':
>          print b
>      if sys.argv[1] == 'c':
>          print c
>
> I run python file.py a and returns the var a, so far so good.
>
> The problem is that i have a bunch of vars(like a to z), how I can
> handle this in a pythonic way?

Use a dictionary.

myVars = {
'a':'This is A',
'b':'This is B',
'c':'This is C',
# as many more as needed
}

print myVars.get(sys.argv[1], 'No variable found')

Is that what you mean?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list