亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用戶輸出分析器將以元音打印的程序

用戶輸出分析器將以元音打印的程序

蝴蝶不菲 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 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號