【发布时间】:2015-11-08 09:34:42
【问题描述】:
我必须为我正在学习的介绍性编程课程编写一小段代码 - 在我尝试这个之前我没有遇到任何问题。据我所知,该代码运行良好,但我不断收到 IndexError,谁能告诉我我错过了什么?
vowel = {"a", "e", "o", "i", "u", "A", "E", "I", "O", "U"}
word = input("Enter a phrase: ")
if word[0] in vowel:
print("an", word)
else:
print("a", word)
编辑: 代码现在运行良好,我只需要根据您的建议为空字符串添加额外的 if 语句
【问题讨论】:
-
什么时候出现错误?是特定的输入(由于您不首先检查单词是否为空字符串,因此尝试索引第一个字符将引发索引超出范围错误)?
-
显然发生在第 4 行?
标签: python indexing conditional-statements