[Tutor] python
Jack Trades
jacktradespublic at gmail.com
Sun Aug 14 22:44:46 CEST 2011
On Sun, Aug 14, 2011 at 8:04 AM, je.rees e-mail <je.rees at virgin.net> wrote:
> I have made a small program but I would like to know how to write or. There
> is multiple choice answer.
> Good=raw_input("Good to hear")
> ok=raw_input("Good good")
> Bad=raw_input("Oh dear")
> I would only like the person using the program to be able to pick one.
>
> Thanks
>
You might want to try something like this...
def ask():
ans = raw_input("Please enter (g)ood, (o)k or (b)ad: ")
if ans == "g" or ans == "good":
print "Good to hear"
elif ans == "o" or ans == "ok":
print "Good good"
elif ans == "b" or ans == "bad":
print "Oh dear"
else:
print "Incorrect choice"
ask()
And here's how it works...
>>> ask()
Please enter (g)ood, (o)k or (b)ad: b
Oh dear
>>> ask()
Please enter (g)ood, (o)k or (b)ad: o
Good good
>>> ask()
Please enter (g)ood, (o)k or (b)ad: ok
Good good
Or you could also use something a bit more general like this...
def ask(question, list_of_responses):
ans = raw_input(question)
return list_of_responses[int(ans)]
>>> ask("(0)Good (1)OK (2)Bad: ", ["Good to hear", "Good good", "Oh dear"])
(0)Good (1)OK (2)Bad: 0
'Good to hear'
--
Jack Trades <http://www.rentageekit.com>
Pointless Programming Blog <http://pointlessprogramming.wordpress.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110814/4db57867/attachment-0001.html>
More information about the Tutor
mailing list