为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

蝴蝶不菲 2021-10-26 18:05:19
我想打印一个用户输入,我的输出将以元音形式出现。结果将是:输入一句话:我叫迈克你想输入更多吗:是输入一句话:我来自中国是否要输入更多:否[a,e,i,i,e,i,a,o,i,a]
查看完整描述

2 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

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.vowels


ob1 = PrintVowels()

print (ob1.get_vowels())


#output['e', 'o', 'o', 'e', 'o', 'o']


查看完整回答
反对 回复 2021-10-26
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

既然你提到用户输入应该只是yes/noor YES/NO,我还添加了一些使用输入和验证它的方法。


def validate_input(inp2):

    if inp2 == 'yes' or inp2 == 'YES' or inp2 == 'no' or inp2 == 'NO':

        return True

    else:

        return False


def 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.vowels


ob1 = PrintVowels()

print (ob1.get_vowels())

输出:


Enter a sentence: 

my name is Mike

Do you want to input more?

blahhhhWHAAT

Error: Please try again with a valid input!

Do you want to input more?

oops

Error: Please try again with a valid input!

Do you want to input more?

yes

Enter a sentence: 

I am from China

Do you want to input more?

Huhhhh

Error: Please try again with a valid input!

Do you want to input more?

NO

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


查看完整回答
反对 回复 2021-10-26
  • 2 回答
  • 0 关注
  • 212 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号