Thread: String ....

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    51
    Rep Power
    10

    String ....


    Hello all,
    Can anybody out there explain to me how to prompt a user to input a string a limited amount of time???
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The simplest way to do this would be to use a for or while loop. The for loop is probably the best since you don't have to increment any variables yourself however this could vary depending on exactly what you wanted to do.

    Code:
    >>> for each in range(5):
    ...     userInput = raw_input('Enter a string with more than 3 characters. ')
    ...     if len(userInput) > 3:
    ...         break
    ... 
    Enter a string with more than 3 characters. a
    Enter a string with more than 3 characters. ab
    Enter a string with more than 3 characters. abc
    Enter a string with more than 3 characters. abcd
    >>> userInput
    'abcd'
    >>>
    Code:
    >>> c = 0
    >>> userInput = ''
    >>> 
    >>> while (c < 5) and not (len(userInput) > 3):
    ...     userInput = raw_input('Enter a string with more than 3 characters. ')
    ...     c = c + 1
    ... 
    Enter a string with more than 3 characters. a
    Enter a string with more than 3 characters. ab
    Enter a string with more than 3 characters. abc
    Enter a string with more than 3 characters. abcd
    >>>
    Ideally I would put one of these in a function and return the input when it matches what you want you can make this more reusable by passing another function to decide if the input is good .

    Code:
    #!/usr/bin/env python
    
    def ask(prompt, match, times = 5):
        for each in range(times):
            userInput = raw_input(prompt)
            if match(userInput):
                return userInput
                
    if __name__ == '__main__':
        ask('Enter a string with more than 3 chatacters. ',
                lambda x: len(x) > 3)
    Hope this helps,

    Mark.
    Last edited by netytan; March 30th, 2005 at 01:12 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    10

    Thumbs up Time or number of times?


    Hi,
    Well looking at the previous post, maybe I'm reading the question wrong, but if you are indeed looking for things involving time constraints, there's plenty of 'time' items in the index:
    http://www.python.org/doc/1.5.2p2/lib/genindex.html#letter-t

    If you are indeed looking for user input for a certain number of times, I would definitely go with a 'while' loop too, no doubt.

    Originally Posted by pord_80
    Hello all,
    Can anybody out there explain to me how to prompt a user to input a string a limited amount of time???
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You're link actually points at the Python 1.5 documentation, there have been a lot of changed since then and the chances of somone running this version still is pretty low. You can find the 2.4.1 docs here:

    http://www.python.org/doc/2.4.1/modindex.html

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    51
    Rep Power
    10

    Thank you guys...


    Thank you very much guys for the detailed explanation....
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    10
    That's weird! I'm using 2.4, I wonder how the heck I got on to the 1.5 docs. I'm pretty sure no one is using 1.5 much now also. .....1.5....*shudder*...
    Originally Posted by netytan
    You're link actually points at the Python 1.5 documentation, there have been a lot of changed since then and the chances of somone running this version still is pretty low. You can find the 2.4.1 docs here:

    http://www.python.org/doc/2.4.1/modindex.html

    Mark.

IMN logo majestic logo threadwatch logo seochat tools logo