如何编写代码让用户选择要删除的元音?(Python)

2024-09-29 22:22:39 发布

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

我正在为最后一个项目编写代码,这是一个允许用户设计自己车牌的应用程序。你知道吗

我想写一些代码,基本上允许用户输入一个英语单词(长度小于或等于10个字符),然后应用程序问他是否要删除单词中的特定元音,然后应用程序输出最后的单词。你知道吗

因为我是一个初学者,我只知道如何编写代码,将省略所有元音在任何用户输入。你觉得怎么样?你知道吗

到目前为止我尝试的代码:

keepOrDeleteVowel1 = input("Would you like to delete the vowels?  Type 'yes' to delete vowels, or 'no' to type a new word.")
                if keepOrDeleteVowel1 == "no" or "No" or "NO":
                    print("This is your word: " + original +  "." + " Enjoy your new license plate! Thank you for using this app.")
                    break
                elif keepOrDeleteVowel1 == "yes" or "Yes" or "YES":
                    firstLetter = original[0]
                    lastLetter = original[len(original)]
                    if firstLetter != "A" or firstLetter != "E" or firstLetter != "I" or firstLetter != "O" or firstLetter != "U" \
                    or lastLetter != "A" or lastLetter != "E" or lastLetter != "I" or lastLetter != "O" or lastLetter != "U":
                        original =original.remove("A")
                        original =original.remove("E")
                        original =original.remove("I")
                        original =original.remove("O")
                        original =original.remove("U")
                        print (original)

抱歉格式化了。上面的代码应该可以删除所有元音,但只有在单词没有以元音开头或结尾的情况下。但是我想改变这一点,允许用户自己删除元音,而不是让程序删除所有元音。你知道吗

请详细回复,非常感谢。你知道吗


Tags: orto代码用户you应用程序delete单词
2条回答
initial_word = input("Type the word: ")
vowel = input("Do you want to remove any vowel? If yes, type the vowel you want to remove: ")
if type(vowel) is str:
    if len(vowel) == 1:
        initial_word = initial_word.replace(vowel.lower(), '')
        initial_word = initial_word.replace(vowel.upper(), '')
    else:
        print('Wrong input.')
print('The word is: ' + initial_word)

输出:

Type the word: AaxeyEizIoucU
Do you want to remove any vowel? If yes, type the vowel you want to remove: e
The word is: AaxyizIoucU

您可以通过input()向用户请求元音,方法与向用户请求原始单词的方法相同。当你保存到一个变量,你可以检查,以确保他们已经输入了元音,如果这样,你可以继续并删除它,如我下面所示。为了避免在比较时用小写和大写输入每个元音,我用lower()将它们的输入转换成小写,您也可以对“no”这样做。你知道吗

vowel = input("Please, type the vowel.")
if vowel.lower() in "aeiou":
    newOrig = original.replace(vowel, "")
    print(newOrig)

else: 
    print("You have not entered a vowel.")

相关问题 更多 >

    热门问题