Providing Default Value for User Input

Discussion in 'Python' started by N?ant Humain, Jan 11, 2004.

  1. N?ant Humain

    N?ant Humain Guest

    I have just begun learning Python so that I can write a simple script
    to make modification of a file used by another Python script easier.
    This file is basically a list of regular expressions. What I want to
    do is allow the user to select one of these regular expressions to
    modify, but I've realized I know of no way to provide a default value
    for user input. I could choose to show the regular expression the user
    has chosen and simply allow the user to retype it and modify it from
    there, but that is time consuming and error prone. Does Python support
    a way to do this? If worse comes to worst, is there a way I could
    write such code on my own without having to write a C-based module
    (I'd like to stick to Python code only since some users will be
    running this script on Windows without a C compiler)?
     
    N?ant Humain, Jan 11, 2004
    #1
    1. Advertising

  2. N?ant Humain wrote in message ...
    >I have just begun learning Python so that I can write a simple script
    >to make modification of a file used by another Python script easier.
    >This file is basically a list of regular expressions. What I want to
    >do is allow the user to select one of these regular expressions to
    >modify, but I've realized I know of no way to provide a default value
    >for user input. I could choose to show the regular expression the user
    >has chosen and simply allow the user to retype it and modify it from
    >there, but that is time consuming and error prone. Does Python support
    >a way to do this? If worse comes to worst, is there a way I could
    >write such code on my own without having to write a C-based module
    >(I'd like to stick to Python code only since some users will be
    >running this script on Windows without a C compiler)?


    This question is entirely too vague, because the answer depends entirely
    upon your implementation and has nothing to do with Python per se. I
    imagine the answer is pretty simple unless your design is horrible.

    As a shot in the dark, why not just look at what the user types? If it's
    something you want to interpret as 'use default' (like just an empty line,
    or the letter 'd' or something), then use a default!

    E.g.:

    choices = dict(a=1, b=2, default=100)
    while True:
    print 'Please select:'
    for k,v in choices.items():
    print '%-10s%s' % (k,v)
    input = raw_input('?> ').lower()
    if input == 'd':
    input = 'default'
    try:
    print 'Value of %s is %s' % (input, choices[input])
    except KeyError:
    print 'Item %r not found.' % input
    print


    If you're doing a lot of command-oriented input loops, look at the cmd
    module, which is pretty handy (I use it quite a bit).
    --
    Francis Avila
     
    Francis Avila, Jan 11, 2004
    #2
    1. Advertising

  3. N?ant Humain

    Miki Tebeka Guest

    Hello,
    > I have just begun learning Python so that I can write a simple script
    > to make modification of a file used by another Python script easier.
    > This file is basically a list of regular expressions. What I want to
    > do is allow the user to select one of these regular expressions to
    > modify, but I've realized I know of no way to provide a default value
    > for user input.

    Which user input? raw_input, Tkinter, wxPython ...

    > I could choose to show the regular expression the user
    > has chosen and simply allow the user to retype it and modify it from
    > there, but that is time consuming and error prone. Does Python support
    > a way to do this?

    To do what? I don't understand your question. Maybe an example will
    help.

    > If worse comes to worst, is there a way I could
    > write such code on my own without having to write a C-based module
    > (I'd like to stick to Python code only since some users will be
    > running this script on Windows without a C compiler)?

    If you create a C extension you can distribute just the binary so your
    users won't need a C compiler. (They might need some required DLL's
    but you can ship them as well).

    HTH.
    Miki
     
    Miki Tebeka, Jan 11, 2004
    #3
  4. Francis Avila wrote in message <>...
    >This question is entirely too vague, because the answer depends entirely
    >upon your implementation and has nothing to do with Python per se.

    ....
    >
    >As a shot in the dark, why not just look at what the user types? If it's


    As Miki showed me, even is saying the question is too vague, I *already*
    assumed too much in my answer to you, namely, that you were using a CLI. I
    don't even know that.

    So you see the problem with your question? We're glad to help, but you need
    to give us more specific information.
    --
    Francis Avila
     
    Francis Avila, Jan 11, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hoegje
    Replies:
    2
    Views:
    22,552
    Gianni Mariani
    Dec 5, 2003
  2. N?ant Humain

    Providing Default Value for User Input

    N?ant Humain, Jan 11, 2004, in forum: Python
    Replies:
    0
    Views:
    301
    N?ant Humain
    Jan 11, 2004
  3. planetthoughtful

    Providing 'default' value with raw_input()?

    planetthoughtful, Dec 22, 2005, in forum: Python
    Replies:
    21
    Views:
    1,203
    Ernst Noch
    Dec 29, 2005
  4. AKS
    Replies:
    11
    Views:
    187
    Thomas 'PointedEars' Lahn
    Apr 4, 2008
  5. Replies:
    0
    Views:
    551
Loading...

Share This Page