[Tutor] func-question_y_n.py
Christopher Emery
cpe.list at gmail.com
Sun Mar 17 03:03:14 CET 2013
Hello All,
OS = Raspbain Wheezy & Ubuntu 12.10 (both updated daily)
Python Version = 3.2 & 3.3
Python Understanding = Beginner (very basic - just started)
Code does not give any errors.
Okay here is my code for func-question_P_N() it has three args to pass
though the function "question", "Positive" (Yes, True, etc) and last
"Negative" (No, False, etc)
1. Take question from argument and present to user
2. Get user response to question and check if match argument to see if
P or N if True then exist function.
3. If False go back to step 1. ask question again as user didn't enter
either the P or N
### Start of Code ###
def question_P_N(question, p, n):
p = p.upper()
n = n.upper()
answer = input(question).upper()
while answer not in (p, n):
print("Your response was", answer + "!", "Please enter", p,
"or", n, "for your response!")
answer = input(question).upper()
return answer
answer = question_P_N("You want a drink of water? :", 'YES', 'no')
print("Your answer to the question was ", answer)
### End of Code ###
Any improvements I should consider and why? I didn't realize that
even for the most basic function of a program can be a learning
experience. Notice I added another part to this program because I
realize what if the arguments was enter as lower case or mix case then
it would try and match the users response that was convert to upper
case to the argument of the program designer (func-question_P_N("Is 1
+ 1 = 3? ", 'yEs', 'nO') this would never work without adding a
converter to the var of p and n before matching to the upper case
string
Thank ahead of time for any advise!
Sincerely in Christ,
Christopher
More information about the Tutor
mailing list