2017-05-12 4 views
0

私は宿題プログラムのほとんどすべてを終えました。最後の機能は、入力にある特定の母音を表示するようにプログラムを設定することです。どのように特定の母音が単語に表示されますか

Please enter a word: look 
The vowels in your word are 
o 
o 
there were 2 vowels 
I'm terribly sorry if I missed any 'y's. 

コード:たとえば

def main(): 
    vowels = ["a","e","i","o","u"] 
    count = 0 
    string = input(str("please enter a word:")) 
    for i in string: 
     if i in vowels: 
      count += 1 

    print("The vowels in your word are:") 

    print("There were",count,"vowels") 
    print("Sorry if I missed any 'y's") 

if __name__ == "__main__": 
    main() 
+0

さて、あなたは、なぜそれらを印刷しない、適切なタイミングで各母音へのアクセス権を持っていますか? –

+0

あなたは2つのオプションがあります:1)母音を(ループで)見つけたときに母音を印刷するか、2)母音をリストに集めて後で印刷する。 – Linuxios

+0

母音から出現数までの辞書マッピングへの母音を変更しようとします。 – afifit

答えて

2

あなたが不足しているすべては、あなたがそれらを見つけると母音の文字列を維持することです。これは母音を数えるようなものです。文字列の "基本値"、つまり空文字列で開始します。母音を見つけるたびに、あなたの文字列に母音を追加(連結)します。たとえば、

vowels_found = "" 
for i in string: 
    if i in vowels: 
     vowels_found += i 
print(vowels_found) 
print(len(vowels_found)) 

この後、vowels_foundを計画した場所に印刷します。投稿されたサンプルのように別々の行に表示したい場合は、をそれぞれの中に印刷し、vowels_foundを一切使用しないでください。

Pythonでこれを行うより直接的な方法があります:このルーチンは基本的に2行の長さになるようにフィルタリングを組み込むことができます:母音を収集するものと、数えて印刷するもの。授業の後半では心配していますが、誰かがその解決策を投稿したら注意してください。 :-)

0

ifにprintステートメントを入れることができます。母音が見つかると、あなたの質問に表示された方法で印刷されます。

NBあなたのifの前に、print("The vowels in your word are:")を移動して母音の前に印刷する必要があります。

例えば

def main(): 
    vowels = ["a","e","i","o","u"] 
    count = 0 
    string = input(str("please enter a word:")) 
    print("The vowels in your word are:") #puts text before the vowels printed in `if` 
    for i in string: 
     if i in vowels: 
      count += 1 
      print (i) #prints the vowel if it is found 



    print("There were",count,"vowels") 
    print("Sorry if I missed any 'y's") 

if __name__ == "__main__": 
main() 
関連する問題