[Tutor] Fwd: Sys.argv read parameters

Alan Gauld alan.gauld at btinternet.com
Thu Apr 18 19:52:01 CEST 2013


On 18/04/13 18:14, Danilo Chilene wrote:

> for command in commands:
>      if arg in commands:
>          print commands[command]
>      else:
>          print 'Invalid command'

I don't understand what you are doing here? Its almost a random 
selection mechanism? You print the command for every item in commands if 
arg is in commands and Invalid Command for every command if its not?
Whatever its trying to do its massively inefficient.

I thought you'd have wanted something like

  for command in commands:
       if arg == command:
           print commands[command]
       else:
           print 'Invalid command'

Which is roughly equivalent to:

print commands.get(arg, 'Invalid command')

but much less efficient!


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



More information about the Tutor mailing list