Hello, I am doing a school project and need assistance on validating raw input, this is very simple stuff, but can someone answer this for me?

The statement is:

ordertype = str(input("Is the order pickup or delivery?"))

Only two values could be entered: P or D (or p or d), if something else is entered, (eg. K) then a statement like "You can only enter P or D" should be displayed, and the question should be asked again.
I have tried the whole if in list thing, but it didn't seem to work.

Could someone please post the answer to this in Code?

4 Years
Discussion Span
Last Post by M.S.

I've also tried this:

ordertype = ""

while ordertype != "P" and ordertype != "D" :
    order = str(input("Is the order for pickup or delivery?"))
    ordertype = order.upper()

This works so far, but I can't figure out how to make it say "Error, you can only enter P or D" if the value entered is not P or D.

ordertype =  raw_input(" Is the order for pickup or delivery? ")

while True:
    if not (ordertype.lower() in ["p","d"]):
        print "No, Enter only P or D!"
        ordertype =  raw_input("Is the order for pickup or delivery? ")
        print "Ok, %s Entered" %ordertype
Votes + Comments
Excellent Response!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.