1 回答

TA貢獻1807條經驗 獲得超9個贊
問題是您將 char 與 'a' 進行比較,然后僅檢查字符串值是否存在,這是一個值并且在這種情況下始終為真。
def count_vowels(txt):
count=0
txt = txt.lower()
for char in txt:
if char == "a" or "e" or "i" or "o" or "u":
count = count+1
print(count)
count_vowels(mark)
你需要做:
def count_vowels(txt):
count=0
txt = txt.lower()
for char in txt:
if char == "a" or char == "e" or char == "i" or char == "o" or char == "u":
count = count+1
print(count)
count_vowels(mark)
或者更清潔的選擇:
def count_vowels(txt):
count=0
txt = txt.lower()
for char in txt:
if char in ['a', 'e', 'i', 'o', 'u']:
count = count+1
print(count)
count_vowels(mark)
添加回答
舉報