简体   繁体   中英

Case insensitive user input

pizza = str(input('Which size pizza do you want: S/M/L/XL?'))

while pizza != "S/M/L/XL":
    print(" That is not a valid answer")
    pizza = "S/M/L/XL"
    pizza = str(input("Which size pizza do you want: S/M/L/XL?"))

This is what I have buts its dosent work

Make the list of allowable characters a set, and check if the input is in that set. Also, force the input to an uppercase character, so the user does not have to worry about it.

Also, having str on your input is redundant, input returns a string by default.

pizza = input('Which size pizza do you want: S/M/L/XL?').upper()

while pizza not in ('S','M','L','XL'):
    print(" That is not a valid answer")
    pizza = input("Which size pizza do you want: S/M/L/XL?").upper()
pizza = str(input('Which size pizza do you want: S/M/L/XL?'))

while pizza != "S" and pizza != "M" and pizza!="L" and pizza!="XL"and pizza!="s"and pizza!="m"and pizza!="l"and pizza!="xl":
    print(" That is not a valid answer")
    pizza = str(input("Which size pizza do you want: S/M/L/XL?"))
print(pizza.upper())

is that what you want?

Try this. I wrote the code to a function.

def asking():
    pizza = str(input('Which size pizza do you want: S/M/L/XL? ')).upper()
    if pizza != 'S' and pizza != 'M' and pizza != 'L' and pizza != 'XL':
        print('That is not a valid answer!')
        asking()
    else:
        print('Okay, your pizza\'s size will be: {}'.format(pizza))

asking()

I would advise you to not reuse variable names. It'll help you truly 'name' things. Is 'pizza' the best name for the variable? How about 'size' or 'order'? This will help you spot bugs more easily. Let's rename the variables.

size_of_pizza = str(input('Which size pizza do you want: S/M/L/XL?'))

while size_of_pizza != "S/M/L/XL":
    print(" That is not a valid answer")
    pizza = "S/M/L/XL"  # See? Here you're manipulating the value of 'pizza' inside a 'while' loop. That's not advised, you can remove this line
    size_of_pizza = str(input("Which size pizza do you want: S/M/L/XL?"))

Let's remove that line. We end up with:

size_of_pizza = str(input('Which size pizza do you want: S/M/L/XL?'))

while size_of_pizza != "S/M/L/XL":
    print(" That is not a valid answer")
    size_of_pizza = str(input("Which size pizza do you want: S/M/L/XL?"))

Oh, but note that you're asking if size_of_pizza != "S/M/L/XL". That's not right! You have several correct answers, so you either need several 'if' clauses or some type of list or iterator. How about this...

size_of_pizza = str(input('Which size pizza do you want: S/M/L/XL?'))

while size_of_pizza not in ["S","M","L","XL"]:
    print("That is not a valid answer")
    size_of_pizza = str(input("Which size pizza do you want: S/M/L/XL?"))

Much better! But you still have work to do! Let me ask you:

  • What happens if the user inputs a number (You may have already thought of this but you should be aware of this decision!)
  • Should size_of_pizza always be forced to become a string?

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM