gpt4 book ai didi

python - 提示用户直到输入 5 个独特的元音或辅音

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:02 25 4
gpt4 key购买 nike

代码的目的在下面得到了最好的解释,例如,如果我输入“hello”,然后输入“luck”,然后输入“liar”,正确的输出应该是“5”“eouia”3“和”ckr“。然而,当我这样做时,它会在输入“骗子”后不断提示我输入更多信息。

        ###########################################################
# Algorithm
# Ask for a word
# Count and store all vowels in the words (repeats only are
# counted once)
# count and store only consonants after the last vowel, if the
# if last letter is a vowel no consonants are stored
# once all 5 vowels are stored or at least 5 consonants are
# stored
# print
# what vowels appear and how many
# what consonants appear
###########################################################

VOWELS = 'aeiou'

word = input('Input a word: ')
wordlow = word.lower() #converts the input to all lowercase

vowcount = 0
concount = 0

vowcollected_str = ''
concollected_str = ''

#ends the program once 5 vowels or consonants have been stored
while vowcount <= 4 and concount <= 4:
vowcount = 0
concount = 0

#stores the actual letter that is being stored, not just how many
vowcollected_str = ''
concollected_str = ''

for i, ch in enumerate(wordlow):
if ch in VOWELS:
if ch not in vowcollected_str:
vowcollected_str += ch
position = i
vowcount += len(vowcollected_str)
if ch not in VOWELS:
if ch not in concollected_str:
concollected_str += wordlow[i:]
concount += len(word[i:])
word = input('Input a word: ')
wordlow = word.lower()
print(vowcount)
print(vowcollected_str)
print(concount)
print(concollected_str)

最佳答案

我会保留一个 set的所有必需的元音,并减去您输入的所有字母,直到集合为空:

VOWELS = 'aeiou'

vowelsSoFar = set(VOWELS)

while vowelsSoFar:
word = input('enter a word: ')
vowelsSoFar -= set(word)
# Feel free to print out the remains vowels, for example

关于python - 提示用户直到输入 5 个独特的元音或辅音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46394918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com