[Tutor] Loops and User Input

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 2 Mar 2001 13:36:11 -0800 (PST)


On Fri, 2 Mar 2001, Marty Pitts wrote:

> However, when I add user input to let the range be dependant on this
> input like this:
> 
> multiplier = input("Which mulitplier value do you want to use? ")
> for i in range(2, multiplier + 1):
>     for j in range(1, multiplier + 1):
>         if i == 2 and j == 1:
>             print "---------------------------------- "
>             print "This table is the",i,"multiple table"
>             print "---------------------------------- "
>         print "%d x %d = %d" % (j, i, j * i)
>     if i == multiplier:
>         break
>     print "---------------------------------- "
>     print "This table is the",i + 1 ,"multiple table"
>     print "---------------------------------- "
> 
> Using any input just returns to a prompt in 'idle'.

Hmmm... actually, this looks ok!  It might just be that the number that
you inputted is too low.  range() is a little weird because its
asymmetric: given something like:

    range(a, b)

it'll construct the list:

    [a, a+1, a+2, ... b-1]

For example:

###
>>> range(2, 2)
[]
>>> range(2, 3)
[2]
>>> range(2, 4)
[2, 3]
###

This is sometimes counter to what people expect; they might expect the
right endpoint to also be a part of the list.

Try it again; it seems to work for me.  Good luck to you!