用户输出分析器将以元音打印的程序

我想打印一个用户输入,我的输出将以元音形式出现。


结果将是:


输入一句话:我叫迈克

你想输入更多吗:是

输入一句话:我来自中国

是否要输入更多:否

[a,e,i,i,e,i,a,o,i,a]


蝴蝶不菲
浏览 213回答 2
2回答

跃然一笑

class PrintVowels():    def __init__(self):        self.vowels = []    def get_vowels(self):        while True:            print("Enter a sentence: ")            inp = input()            self.vowels.extend([i for i in inp.lower() if i in 'aeiou'])            print("Do you want to input more?")            inp2 = input()            if inp2.lower() == 'no':                break        return self.vowelsob1 = PrintVowels()print (ob1.get_vowels())#output['e', 'o', 'o', 'e', 'o', 'o']

鸿蒙传说

既然你提到用户输入应该只是yes/noor YES/NO,我还添加了一些使用输入和验证它的方法。def validate_input(inp2):    if inp2 == 'yes' or inp2 == 'YES' or inp2 == 'no' or inp2 == 'NO':        return True    else:        return Falsedef take_input(self):    print("Enter a sentence: ")    inp = input()    self.vowels.extend([i for i in inp.lower() if i in 'aeiou'])class PrintVowels():    def __init__(self):        self.vowels = []    def get_vowels(self):        take_input(self)        while True:            print("Do you want to input more?")            inp2 = input()            valid_inp = validate_input(inp2)            if valid_inp:                if inp2.lower() == 'no':                    break                else:                    take_input(self)            else:                print("Error: Please try again with a valid input!")                continue        return self.vowelsob1 = PrintVowels()print (ob1.get_vowels())输出:Enter a sentence: my name is MikeDo you want to input more?blahhhhWHAATError: Please try again with a valid input!Do you want to input more?oopsError: Please try again with a valid input!Do you want to input more?yesEnter a sentence: I am from ChinaDo you want to input more?HuhhhhError: Please try again with a valid input!Do you want to input more?NO['a', 'e', 'i', 'i', 'e', 'i', 'a', 'o', 'i', 'a']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python