简体   繁体   English

Python-在!=中使用多个单词

[英]Python - using more than one word in !=

I want to use more than one word in !=, but I can't for the life of me understand or google my way how to. 我想在!=中使用多个单词,但我一生无法理解或用Google搜索该怎么做。 Can anyone help, and explain why I can't just do it like: != "No" != "no" !="n" !="N": etc etc 任何人都可以帮忙,并解释为什么我不能做这样的事情:!=“否”!=“”否“!=” n“!=” N“:等等

answers = ["Yes", "Not now not ever.", "Unclear answer, try again.", "Maybe.", "Probably not.", "Try again later.",
           "My sources says no.", "My sources says yes.", "Only when there is a full moon.", "It is certain.",
           "Cannot predict now.", "Outlook not so good", "Very doubtful.", "You may rely on it.",
           "Yes - definitely.", "As I see it, yes.", "Signs point to yes."]    

while str(input("Do you want to ask the Magic 8Ball a question?  Yes or No?  ")) != "No" != "no": # <-- problematic line
    str(input("Ask the mighty 8Ball your question:  "))
    randomanswer = answers[random.randint(0, len(answers) -1)]
    print("\n","The Magic 8Ball says: ")
    print(randomanswer, "\n")    
else:
    return

You can use a tuple of sentinel-values, eg 您可以使用标记值的元组,例如

while foo not in ('No', 'no', 'N', 'n'):
    # code

or 要么

while foo.lower() not in ('no', 'n'):
    # code

If there are a huge amount of sentinels, a set will perform faster than a tuple (for low amounts of values, hashing is not worth it). 如果有大量的哨兵,则set执行速度将比元组快(对于少量值,散列是不值得的)。

You might also want to look into the re module for more sophisticated pattern matching. 您可能还需要研究re模块以获得更复杂的模式匹配。

尝试

while str(input("Do you want to ask the Magic 8Ball a question?  Yes or No?  ")) not in ["No", "no"]

You're looking for a way to chain together conditional statements . 您正在寻找一种将条件语句链接在一起的方法。 Unfortunately you cannot do it the way you're trying to do. 不幸的是,您无法以尝试的方式进行操作。 However, you can use and and or to chain them together, which is how it's normally done. 但是,您可以使用andor将它们链接在一起,这通常是完成的。 For example: 例如:

userinput = str(input("Do you want to ask the Magic 8Ball a question?  Yes or No?  "))

while userinput != "No" and userinput != "no":
    # have to get user input again at end of loop

But an even better approach is to find how to logically turn this into a single conditional, in this case using lower on the user input: 但是,更好的方法是找到如何将其逻辑上转换为单个条件,在这种情况下,使用lower的用户输入:

userinput = str(input("Do you want to ask the Magic 8Ball a question?  Yes or No?  "))
userinput = userinput.lower() # make it lowercase

while userinput != "no":
    # have to get user input again at end of loop

Finally, you can use random.choice to provide a random element from your answers array. 最后,您可以使用random.choice从您的答案数组中提供一个随机元素。

Change your problematic line to: 将有问题的行更改为:

while input("Do you want to ask the Magic 8Ball a question?  Yes or No?  ").strip().lower() != "no":

This way, you convert the input string to lowercase ( lower() ) while also stripping any space the user might have typed in ( strip() ) 这样,您可以将输入字符串转换为小写( lower() ),同时还strip()了用户可能在其中键入的任何空格( strip()

Then, if user types: "No", lower() will convert it to all lowercase, "no" and then check it towards your != 'no' . 然后,如果用户键入:“ No”,则lower()会将其转换为所有小写的“ no”,然后将其检查为您的!= 'no'

Anything typed into input() is a string, so can omit the str() . 输入input()任何内容都是字符串,因此可以省略str()

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

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