School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,418 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,564 people online right now. Registration is fast and FREE... Join Now!




Verifying Variables Content

 

Verifying Variables Content

brianmen

19 Jun, 2009 - 07:54 AM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 14



Thanked: 1 times
My Contributions
Hello... So I am making a program where I want people to vote on something. I have them answer a question with either an A or a B. I do this using the

CODE
inputtedanswer=raw_input("This is the question, now you should answer A or B")


method. I want to make it so that only answers that are A or B (or even a or b lowercase) can be accepted as answers and not anything else. Is there a way I can do this? Thanks for your help.

User is offlineProfile CardPM
+Quote Post


shadhin

RE: Verifying Variables Content

19 Jun, 2009 - 08:08 AM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Sep, 2008
Posts: 421



Thanked: 19 times
Dream Kudos: 3650
My Contributions
You can try this format-
CODE

z=raw_input("Enter Answer:")
if z=="A" or z=="a":
    print "AAA"

elif z=="B" or z=="b":
    print "BBB"

User is offlineProfile CardPM
+Quote Post

code_m

RE: Verifying Variables Content

19 Jun, 2009 - 09:12 AM
Post #3

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
another way to write the same thing:

python

line = raw_input("Enter Answer:")
if line in ("A", "b"):
print "AAA"
elif line in ("B", "b"):
print "BBB"


and the way I would write it:
python

def enter_ans(str):
while True:
line = raw_input(str).lower()
if line == "a":
return "AAA"
elif line == "b":
return "BBB"
else:
print "Error: Unknown answer: " + line
continue

User is offlineProfile CardPM
+Quote Post

brianmen

RE: Verifying Variables Content

20 Jun, 2009 - 10:49 AM
Post #4

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 14



Thanked: 1 times
My Contributions
Thanks, I'll try these out!!!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:36PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month