简体   繁体   English

从输入Python3查找大写小写单词。

[英]Finding uppercase lowercase words from an input, Python3.

I'd like to enhance this Python3 code (shortened) so that it can also find the word GreeTinGs (without matching the case). 我想增强此Python3代码(缩短),以便它也可以找到单词GreeTinGs(不区分大小写)。 The data I will be using will be a mess of upper and lowercase. 我将使用的数据将是大小写混乱的。 I'd like the user to type in words that also could contain upper and lowercase letters (anywhere in the word). 我希望用户输入还可以包含大写和小写字母的单词(单词中的任意位置)。 The data cannot be converted to all lower or all upper. 数据无法转换为全部较低或全部较高。 Ref: it can find 'HeLLo' but not 'GreeTinGs'. 参考:它可以找到“ Hello”,但找不到“ GreeTinGs”。

Currently the "casefold" command fits most uses apart from where; 目前,“ casefold”命令适合除位置之外的大多数用途; the data has uppercase letters in the middle of the word. 数据在单词中间有大写字母。 Is there another command like casefold or something else that I can use? 是否还有另一个命令,例如casefold或其他我可以使用的命令? For ref, there will also be another input, that will case sensitive search, this is why I need both options. 对于ref,还将有另一个输入,区分大小写,这就是为什么我需要两个选项。

Here is the code and all help is appreciated: 这是代码,感谢所有帮助:

import random

greetings = ['hola', 'hello', 'GreeTinGs', 'hi', 'Hi', 'hey!','hey']
question = ['How are you?','How are you doing?']
responses = ['Okay',"I'm fine"]

while True:
        userInput = input(">WHAT:").casefold()
        if userInput in greetings:
            greetingsrandom = random.choice(greetings)
            print (greetingsrandom)
        elif userInput in question:
            responsesrandom = random.choice(responses)
            print (responsesrandom)
        else:
            print("I did not understand what you said")

For case insensitivity you could make it all uppercase 对于不区分大小写的情况,可以将其全部大写

import random

greetings = ['hola', 'hello', 'GreeTinGs', 'hi', 'Hi', 'hey!','hey']
question = ['How are you?','How are you doing?']
responses = ['Okay',"I'm fine"]

while True:
        userInput = input(">WHAT:").casefold()
        if userInput.upper() in [x.upper() for x in greetings]:
            greetingsrandom = random.choice(greetings)
            print (greetingsrandom)
        elif userInput in [x.upper() for x in question]:
            responsesrandom = random.choice(responses)
            print (responsesrandom)
        else:
            print("I did not understand what you said")

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

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