Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有结束错误的Python程序_Python_String - Fatal编程技术网

有结束错误的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
)。因为我不希望程序生成“不包含元音”,所以我希望程序不响应@jonRSharpesisnce
count<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
    ...