简体   繁体   English

不区分大小写的用户输入

[英]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.此外,输入中的str是多余的, input默认返回一个字符串。

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? 'pizza' 是变量的最佳名称吗? 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".哦,但请注意,您问的是 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.您有几个正确的答案,因此您需要几个“if”子句或某种类型的列表或迭代器。 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? size_of_pizza 是否应该总是被迫成为一个字符串?

Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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