[Tutor] User input question

alan.gauld@bt.com alan.gauld@bt.com
Thu, 8 Aug 2002 17:24:28 +0100


> The script below is my latest attempt, and the error message 
> I get is below it. 

Hi shey.

To start at the end first...

> i = input('Try a number ')
>     print timestab(i)

This sets i to the number you want.
And you correctly pass it to timestab but timestab 
doesn't return a printable result. It only prints 
things itself. Remove the print as a first step.
Next, you have indented the timestab(which is why 
you get an error). Indentation is all important 
in Python and should only happen after an if test 
or a loop construct or something similar(basically 
after a line witrh a colon(:) at the end...

Now lets look at timetab itself:

> def timestab(n):
>     if i < 13:

The function takes a parameter called n.
Although you passed in an argument value held 
in a variable called 'i' the function seees 
that value as being stored in the parameter, n.

Thus you need to do

if n < 13:

>         print "%d x %d = %d" % (i,n,i*n)

Since i is not defined and n holds whatever 
value i was when you passed it in this won't 
work either. Also it will only print this 
single line not the whole table, for that 
you need a loop of some kind.

Assuming you are using my tutor (looks very like it! :-)
you need to go and look at the function in the tutor 
again. Duplicate that within your function but change 
the value to refer to n.

Finally, there is another way to go about things using 
namespace control described elsewhere in my tutor, but 
in this case its not a very good solution (I don't 
think it would do what you want!) so I won't tell 
you about it(yet)! :-)

HTH

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld