question on input function
Chris Rebert
clp2 at rebertia.com
Mon Jul 20 04:44:45 CEST 2009
On Sun, Jul 19, 2009 at 7:07 PM, Richel Satumbaga<rlsatumbaga at yahoo.com> wrote:
> I am just learning python then I encountered an certain point in terms of
> using the input function of python.
> the source code:
> eq = input("enter an equation:");
> print " the result is : ";
>
>
> the output seen in the command window:
>>>>
> enter an equation:[m*x+b for m in (0,10,1),for x in (1,11,1), for b in
> (2,12,1)]
>
> Traceback (most recent call last):
> File "C:/Python26/try", line 1, in <module>
> eq = input("enter an equation:");
> File "<string>", line 1
> [m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)]
> ^
> SyntaxError: invalid syntax
>
> Is there any other way to enter those? or it must be coded
> in the source code?
> If in source code is it like in matlab where one can type:
>
> x = [0:1:10];
>
> to define the range of x which is form 0 to 10 with the increment of 1.
First, commas between the "for"s are not permitted, which gives us:
[m*x+b for m in (0,10,1) for x in (1,11,1) for b in (2,12,1)]
However, in Python, (i, j, k) is just a tuple (MATLAB: 1D matrix) of
the items i, j, and k.
What you want is a range, which is understandably created using the
range() function.
A step of 1 and a start of 0 are the defaults, and the stop value is
always excluded, so the code becomes:
[m*x+b for m in range(11) for x in range(1,12) for b in range(2,13)]
Which should be what you want.
For further explanation of the range() function:
range(b) - the numbers from 0 to b-1 with a step of 1
range(a,b) - the numbers from a to b-1 with a step of 1
range(a,b,c) - the numbers from a to b-1 with a step of c
Hope that helps.
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list