如何编写允许用户选择要删除的元音的代码? (蟒蛇)

时间:2017-07-02 09:13:26

标签: 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)

抱歉格式化。上面提供的代码应该用于删除所有元音,但仅限于单词不以元音开头或结尾。但我想改变这一点,以便用户自己删除元音,而不是让程序删除所有元音。

请详细回复,非常感谢。

2 个答案:

答案 0 :(得分:1)

您可以通过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.")

答案 1 :(得分:0)

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