[Tutor] double result ...

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Nov 16 19:23:48 EST 2003


>>> a=7
>>> b=9
>>> c=8
>>> for i in range(a,b,c):


Let's look at this more closely.

You called range(7,9,8)

that returns a list of values from 7 to (9-1) separated by 8.
That means you only get one value, 7.

try it by typing range() at the >>> prompt. Experiment with
some values till you understand how it works.

> sequence" for computer (it doesn't support alphabet) !!

Support for the alphabet is another issue and nothing to do
with what's happening here. Lets come back to that later.

>>> a=4
>>> b=6
>>> c=8
>>> for d in range(a,b,c):

This time you created a list from 4 to (6-1) stepping by 8
- again the single value 4

>>> a=4
>>> b=6
>>> c=8
>>> for d in range(b,c):

This time a list from 6 to 8-1, that is 6 and 7.
So you have two values that d is set to and so you print b twice.

What I think you shjould have written was:

for d in [a,b,c]:
  print d

That is, instead of using range() to create a list you just create the
list by hand.

As for letters, you can access them using the predefined sequence in
the
string module:

import string
for char in string.letters:
  print char

for char in string.uppercase:
  print char

and so on. Take a look at the string module to see what other
character sets
are defined.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list