有结束错误的Python程序
我目前正在使用此代码有结束错误的Python程序,python,string,Python,String,我目前正在使用此代码 word = input('Enter a word: ') count = 0 vowels = ['a' , 'e' , 'i' ,'o' , 'u'] for char in word: if char in vowels: count += 1 if count == 1: print(word + ' contains ' + str(count) + ' vowel') elif count > 1: print(
word = input('Enter a word: ')
count = 0
vowels = ['a' , 'e' , 'i' ,'o' , 'u']
for char in word:
if char in vowels:
count += 1
if count == 1:
print(word + ' contains ' + str(count) + ' vowel')
elif count > 1:
print(word + ' contains ' + str(count) + ' vowels')
elif count < 1:
print(word + ' contains ' + str(count) + ' vowels')
elif word == "":
print("")
有人能告诉我我做错了什么或帮我解决吗?如果count==1:
if count == 1:
print(word + ' contains ' + str(count) + ' vowel')
elif count > 1:
print(word + ' contains ' + str(count) + ' vowels')
elif count < 1:
print(word + ' contains ' + str(count) + ' vowels')
elif word == "":
print("")
打印(word+'包含'+str(计数)+'元音')
elif计数>1:
打印(word+'包含'+str(计数)+'元音')
elif计数<1:
打印(word+'包含'+str(计数)+'元音')
elif word==“”:
打印(“”)
您将永远无法到达最后一个
elif
。Count总是1
(如果它是一个数字),因此其中一个if
s将触发并产生打印您以错误的顺序测试事物:对于输入word='
,Count==0
,因此条件Count<1
为True
和elif-word=>
是从未到达的。相反,请重新排列测试顺序:
if not word: # more Pythonic equivalent to 'if word == "":'
print("")
elif count == 1:
print(word + ' contains ' + str(count) + ' vowel')
elif count > 1:
print(word + ' contains ' + str(count) + ' vowels')
elif count < 1:
print(word + ' contains ' + str(count) + ' vowels')
你为什么认为那是错误的<代码>“
不包含元音(在测试word==”
之前测试count<1
)。因为我不希望程序生成“不包含元音”,所以我希望程序不响应@jonRSharpesisncecount<1
,count==1
和count>1
中的一个必须始终为真,在此之后,任何elifs都无法访问。如何使程序继续询问“输入单词”,直到没有输入单词@jonrsharpe@kikiaraab一个而循环?这不是一个代码编写服务。
if not word: # more Pythonic equivalent to 'if word == "":'
print("")
elif count == 1:
print(word + ' contains ' + str(count) + ' vowel')
elif count > 1:
print(word + ' contains ' + str(count) + ' vowels')
elif count < 1:
print(word + ' contains ' + str(count) + ' vowels')
word = input('Enter a word: ')
if word:
count = 0
vowels = set("aeiou") # this makes the program more efficient
...