[英]How do I display what specific vowels are in the word
我已經完成了作業計划中的幾乎所有內容。 最后一個功能是使程序顯示在輸入中找到的特定元音。 例如:
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()
您所缺少的只是找到一串元音。 這很像數元音。 從字符串的“基值”(空字符串)開始。 每次找到元音時,都將其添加(連接)到字符串中。 例如:
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中有更高級,更直接的方法:您可以在單個語句中包括過濾,因此該例程基本上有兩行長:一個用於收集元音,另一個用於計數並打印它們。 擔心稍后再上課...但是如果有人發布了這些解決方案,請注意。 :-)
您可以在if
放入打印語句。 這樣,當找到元音時,它會以您在問題中顯示的方式打印。
注意,您需要將print("The vowels in your word are:")
到if
之前if
以便將其打印在元音之前。
例如
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.