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?

Recommended Answers

All 2 Replies

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? ")
    else:
        print "Ok, %s Entered" %ordertype
        break
commented: Excellent Response! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.