newbie Q: how do you re prompt user?

Nicodemus nicodemus at globalite.com.br
Mon Oct 21 00:04:52 EDT 2002


How about:

-----
from time import sleep

query = "Wanna hire someone who likes Python and likes to learn?"
while True:
    answer = raw_input(query)
    while answer.lower() not in ['y', 'yes', 'ok', 'sure']:
        print 'Please reconsider that answer!'
        answer = raw_input(query)
    print "Great, email Jonathan now!"
    print "he is waiting at xxxxxx.org"
    sleep(300)
    print "Did you email Jonathan yet?"

-----

Key points:

1) You can use the operator 'in' to check if an item is inside a list.

>>> 'yes' in ['yes', 'y']
1
>>> 'aaa' in ['yes', 'y']
0

2) str.lower() returns the original string with all letters converted to lower
case:

>>> 'YES'.lower()
'yes'
>>> 'yEs'.lower()
'yes'

which makes it much more easy to check strings entered by the user, and you don't
want to be too restrictive.

3) Since you want to do some task "forever" (that's what I understood by your
script),
a 'while' loop is recommended.

It seems like you're a newbie in programming, not python in particular. I
recommend the
"Non Programmers Tutorial for Python" by Josh Cogliati, which is included in the
ActivePython
distribution.


Good Luck!
Nicodemus.


----- Original Message -----
From: "Jonathan Driller" <jdriller at orchid.org>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Monday, October 21, 2002 12:13 AM
Subject: newbie Q: how do you re prompt user?


> I am just getting started so a very basic Q:
> If I write something that prompts a user for a response and then use
> the same function later it does not work...how come? I have tried
> encapsulating this in a def and then calling itself at the end but
> that did not work either...
>
> #import sleep function so we can remind hiring manager... ;-)
> from time import sleep
>
> x = raw_input("Wanna hire someone who likes Python and likes to
> learn?")    #use function to get input
> negative = ''   #assignment of empty char to initialize
> positive = ''
>
> #loop thru all no possibilities (crude) and assign n if no
> if x == 'n':
>     negative == 'n'
> elif x == 'No':
>     negative == 'n'
> elif x == 'NO':
>     negative == 'n'
> else:
>     negative = ''
>
> while negative == 'n':
>     print "Please reconsider your choice"
>
>
> #loop thru all yes possibilities (crude) and assign y if yes
> if x == 'y':
>     positive = 'y'
> elif x == 'yes':
>     positive = 'y'
> elif x == 'Yes':
>     positive = 'y'
> elif x == 'YES':
>     positive = 'y'
> else:
>     print "Please reconsider that answer"
>
> while positive == 'y':
>     print "Great, email Jonathan now!"
>     print "he is waiting at xxxxxx.org"
>
>     sleep(300)
>     print "Did you email Jonathan yet?"
>
>     #rerun function
>     x = raw_input("Wanna hire someone who likes Python and likes to
> learn?")
> --
> http://mail.python.org/mailman/listinfo/python-list
>






More information about the Python-list mailing list