我正在嘗試制作一個程式,該程式從用戶那里獲取一個單詞并從元音串列中檢查并列印在該單詞中找到的元音以及找到了多少。這是我到目前為止所擁有的,它非常不正確,但我嘗試了一些東西。
print("This program will count the total number of vowels in the word you enter.")
vowels = ["a", "e", "i", "o", "u"]
userWord = input("Please enter a single word: ")
if vowels in userWord:
print(vowels)
vowelCount = 0
vowelCount = vowelCount 1
print("There are " vowelCount " total vowels in " userWord)
uj5u.com熱心網友回復:
如果您從 Python 開始,您可以做的事情是遍歷輸入單詞中的每個字母。
然后在每次迭代中檢查字母是否在元音串列中,如下所示:
print("This program will count the total number of vowels in the word you enter.")
vowels = ["a", "e", "i", "o", "u"]
userWord = input("Please enter a single word: ")
vowelCount = 0
foundVowels = []
for letter in userWord:
if letter in vowels:
foundVowels.append(letter)
vowelCount = 1
print("There are " str(vowelCount) " total vowels in " userWord ": " str(foundVowels))
uj5u.com熱心網友回復:
使用該sum()函式計算單詞中元音的數量。
vowelCount = sum(vowel in userWord for vowel in vowels)
print("There are {} total vowels in {}".format(vowelCount, userWord))
uj5u.com熱心網友回復:
vowels=['a','e','i','o','u']
count=0
ls=[]
wordfromUser=input("Enter a word: ")
for i in wordfromUser:
if i in vowels:
count =1
ls.append(i)
print("There are " str(count) " total vowels in " wordfromUser)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432197.html
標籤:Python python-3.x 列表
下一篇:將資訊從一個串列添加到另一個串列
