Semi-final code as text...

Alex Martelli aleaxit at yahoo.com
Wed Mar 28 11:25:30 EST 2001


"Ron Stephens" <rdsteph at earthlink.net> wrote in message
news:3ABFF28E.EE28A322 at earthlink.net...
    [snip]
Using 'arrays' (a generic term; in Python, they are called
lists) and loops will save you a lot of repetitive code:

> x = 1
> option1 = raw_input ("what is option1:")
> x = x + 1
> option2 = raw_input ("what is option2:")
> x = x + 1
> while x:
>  if x > number: break
>  option3 = raw_input ("what is option3:")
>  x = x + 1
>  if x > number: break
>  option4 = raw_input ("what is option4:")
>  x = x + 1
>  if x > number: break
>  option5 = raw_input ("what is option5:")
>  if x > number: break
>  option6 = raw_input ("what is option6:")
>  break

Change into:

options = []
for x in range(0, number):
    options.append(raw_input("what is option%s:"%(x+1)))

[and similarly for other 'arrays' that you're simulating
 with bunches of similarly-named variables: criteria1 etc,
 rank1 etc, oneone and onetwo and ... and twoone and ...];

whenever you need to access the i-th option (for example),
you'll write options[i] (where you'd now repeat the code
with option1, option2, etc) -- just remember that i goes
from 0 to N-1 for some N, not from 1 to N.


This will make your code drastically smaller and simpler
and remove artificial limits on number of criteria and
options.


Alex






More information about the Python-list mailing list