Creating a Simple User Interface for a Function

Dave Angel davea at
Thu Jul 25 21:19:27 CEST 2013

On 07/25/2013 12:03 PM, CTSB01 wrote:
> I have the following code that runs perfectly:
>       def psi_j(x, j):
>            rtn = []
>            for n2 in range(0, len(x) * j - 2):
>              n = n2 / j
>              r = n2 - n * j
>              rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
>              print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn
>            return rtn

No it doesn't run perfectly.  It'll get a syntax error on the print 
function call.  That's assuming you're still using Python 3.3.  You 
really need to start by specifying your environment, without making us 
look back through previous threads from you.

> This code takes a string x = [0,1,1,1,2] for example

That's not a string.  A string would be like
    xx = psi_j("0abcd1234")

Perhaps you mean list?  And is it a list of integers, or of arbitrary 
numbers?  Are there any constraints on the sizes or signs of those numbers?

> (it must always begin with 0) and a parameter j, say 2, and outputs a string (x = [0, 1, 2, 2, 2, 2, 2, 3] in this example).
> It does this in two steps: First it decomposes some number m into a multiple of j and a remainder.

Only if you replace the / with //.  Or just use the function divmod():
       n, r = divmod(n2, m)

>  Then it runs this decomposition through a function on the rtn.append line.
> Notice that this has cj - 1 terms where c is the number of terms in the input string and j is the parameter.  Normally, we would like it to be able to calculate cj terms.
> This is an issue with the function that I am more than happy to put aside for the moment.
> My key interest is to be able to make this program

So far you have a function, not a program.  If you put it in a text file 
and run it from python, it'll do nothing but display a syntax error 
message.  And when you fix that, it'll just run without doing anything.

  usable for someone who has no knowledge of programming.  In 
particular, I need some kind of user interface that prompts
> the user to input a string (ideally just by putting in numbers in the form 011123334 for example) and a parameter,
> and then displays the output sequence.  This is essentially what the program already does but the idea is to make it usable
> for even the most technologically disinclined.  Ideally it would do this without needing to run Python at all.

Then why are you asking on the Python forum?  Or perhaps you mean 
without him knowing he's running Python?  In that case, use a shebang 
line at the beginning, which will tell Linux to automatically invoke the 
specified program (or programming language in this case).

>  If anyone is able to make this happen in Python I would be eternally grateful.

If we assume you're running Python 3.3 on Linux, and the user is willing 
to us the terminal, then how about parsing the string from the command 
line he types?  You can access it as011123334 a string from sys.argv, 
and convert it to separate numbers.  Of course as it stands now, you 
cannot tell whether the user wanted
   0, 111, 23, 3, 3, 4

or something else.


More information about the Python-list mailing list