我的程序基本上允许用户输入他们选择的单词,之后,让他们选择删除他们选择的某些元音。
它是这样的:
v = ['A', 'E','I','O','U','AE', 'EA', 'AI', 'IA', 'AO','OA', 'AU','UA',
'EI','IE','EO','OE','EU','UE','IO','OI', 'IU','UI', 'OU','UO',
'AEI','AEU', 'EIO','EIU','IOU', 'AEIO','AEIU','EIOU',
'AEIOU'
]
keepOrDeleteVowel1 = input("Would you like to delete some vowels? Type 'yes' to choose which vowels you would like to delete, or 'no' to type a new word.").upper()
if keepOrDeleteVowel1.upper() == "YES":
initial_word = input("Please type the word again: ")
vowel = input("Type the vowels you want to remove. Tip: If your word begins or ends with a vowel, try not to delete the vowel because the word might not be recognizable.")
if type(vowel) is str:
if len(vowel) <=5 and vowel == v[0-24]:
initial_word = initial_word.replace(vowel.lower(), '')
initial_word = initial_word.replace(vowel.upper(), '')
else:
print('Wrong input.')
print("This is your word: " + initial_word + "." + " Enjoy your new license plate! Thank you for using this app.")
如果您想知道,顶部是用户在选择要从他的单词中删除哪些元音时所有可能输入的列表。
如果你能抓住它,vowel == v[0-24]:
是我尝试访问顶部列表v
中的所有元素的尝试。我该怎么改变?
基本上,如果用户在顶部列表中键入任何元素,比如说,IU&#39;,他就会从原始单词中删除字母I和U.
基本上,if语句的逻辑:
if len(vowel) <=5 and vowel == v[0-24]:
如果用户的响应长度小于或等于5并且用户的输入与列表v
的任何元素匹配....删除元音..你明白了。
我知道如果陈述错了,那我应该把它改成什么?
答案 0 :(得分:1)
将vowel == v[0-24]
替换为vowel.upper() in v
。
答案 1 :(得分:0)
您想测试另一组字母中是否包含一组字母。因此,使用的完美之处是set
,请参阅the documentation in Python built-in types。
我们从字符串中创建一组允许的元音。它将是集合{'U', 'O', 'I', 'A', 'E'}
(集合不是有序的)。
稍后,我们将创建一组您输入的元音(转换为大写),并使用<=
运算符检查该集合是否包含在(或等于)第一个元素中:
>>> set('AUE') <= set('AEIOU')
True
>>> set('ARIO')<= set('AEIOU')
False
然后,我们将迭代我们输入的元音,并在我们的初始单词中替换每一个(大写和小写)。
all_vowels = set('AEIOU')
# {'U', 'O', 'I', 'A', 'E'}
keepOrDeleteVowel1 = input("Would you like to delete some vowels?"
" Type 'yes' to choose which vowels you would like to delete,"
" or 'no' to type a new word.").upper()
if keepOrDeleteVowel1.upper() == "YES":
initial_word = input("Please type the word again: ")
vowels = input("Type the vowels you want to remove."
" Tip: If your word begins or ends with a vowel,"
" try not to delete the vowel because the word"
" might not be recognizable.").upper()
if set(vowels) <= all_vowels:
for vowel in vowels:
initial_word = initial_word.replace(vowel.lower(), '')
initial_word = initial_word.replace(vowel.upper(), '')
print(initial_word)
else:
print('Wrong input.')
作为旁注:由于代码中的字符串很长,我使用Python的自动连接来剪切它们:字符串紧跟在一起简单地连接起来。