如何将用户的输入拆分为单词,但即使存在空格,也允许此代码仍然有效?

2024-04-25 04:00:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着分割用户的输入,以便程序检测到用户输入中的特定单词,然后对其进行操作。我做了一些变量,并给它们指定了用户可能输入的特定关键字。如果关键字前面没有空格,例如pin是随机的,我的程序可以正常工作。但是如果我把pin这个词放在一个句子的中间,它就不能正常工作,例如,我的pin是随机的。 我怎么能修好这个?!在

question1= input("Do you have a problem with your device? Answer with yes or no: ")
if question1== 'no':
    print ("Please exit the troubleshooting program...")
    exit()
elif question1== 'yes':

    userquestion=input("What is your problem?")
    useranswer=userquestion.split()


    key_words1 = ("screen", "display", "monitor")
    key_words2 = ("volume", "sound", "hear", "mute", "music", "speaker", "loudspeaker")
    key_words3 = ("earphones", "headphones")
    key_words4 = ("WiFi", "wifi", "wi-fi", "speed", "internet", "connection", "network")
    key_words5 = ("switch","off", "power", "turn", "charge", "turning",)
    key_words6 = ("forgot", "pin", "forgotten", "password", "locked", "unlocked")
    key_words7 = ("storage", "installing", "memory", "sd", "lags", "lag","lagging", "slow", "install", "apps", "applications", "download")



    for word in useranswer:

        if word in key_words5:
            file = open('Power.txt', 'r')
            print (file.read())
            print()
            print("Thankyou for using this Troubleshooting program. We hope this has helped")
            exit(0)
        elif word in key_words7:
            file = open('Storage.txt', 'r')
            print (file.read())
            print()
            print("Thankyou for using this Troubleshooting program. We hope this has helped")
            exit(0)
        elif word in key_words2:
            file = open('speakers.txt', 'r')
            print (file.read())
            print()
            print("Thankyou for using this Troubleshooting program. We hope this has helped")
            exit(0)
        elif word in key_words4 :
            file = open('wifi.txt', 'r')
            print (file.read())
            print()
            print("Thankyou for using this Troubleshooting program. We hope this has helped")
            exit(0)
        elif word in key_words6:
            file = open('password.txt', 'r')
            print (file.read())
            print()
            print("Thankyou for using this Troubleshooting program. We hope this has helped")
            exit(0)
        elif word in key_words1:
            file = open('screen.txt', 'r')
            print (file.read())
            print()
            print("Thankyou for using this Troubleshooting program. We hope this has helped")
            exit(0)
        elif word in key_words3:
            file = open('headset.txt', 'r')
            print (file.read())
            print()
            print("Thankyou for using this Troubleshooting program. We hope this has helped")
            exit(0)
        else:
            print("Troubleshooting program was unable to detect your problem.")
            reporting=input("Sorry for any inconveniences- you can report you problem here. Would you like to do that?  ")
            if reporting== 'yes':
                print ("Please type in your comment below. Thank you!")
                comment=input("Report comment: ")
                exit(0)
            else:
                print("We will work on improving this program shortly")
                exit(0)
            else:
                print("Please open up the program and try again.")
            exit()

程序只是跳到结束代码。在


Tags: keyintxtforreadexitopenthis
2条回答

您应该使用.strip()方法。

useranswer = userquestion.split()
useranswer = map(str.strip, useranswer)

这将删除单词周围的多余空格。

不同的方法可能是使用pythonre模块,即正则表达式,而不是调用简单的.split(),而可以调用re.split('\s+', userquestion)。它的工作方式类似于普通拆分,但通过正则表达式进行拆分。在本例中,\s表示白色字符,+表示{}字符的重复,至少出现一次,但一直到无限多次。关于re模块here的更多信息。

示例

>>> import re
>>> string = "My fancy   string         with many       spaces\n and tabs    "
>>> re.split('\s+', string)
['My', 'fancy', 'string', 'with', 'many', 'spaces', 'and', 'tabs', '']

您可能已经注意到,此方法的缺点是它还添加了空字符串''。这是因为它将tabs拆分为两个单词tabs和{}。这使得这种方法变得愚蠢。有一些解决方法(其中一个使用.strip(),因此不会给这种方法带来任何新的东西),但是我会给出@YashMehrotra's的答案。但嘿,这也许你到现在还不知道正则表达式。。

相关问题 更多 >