Python Grok学习IndexError类型的异常

时间:2016-08-25 09:59:27

标签: python

我刚刚开始学习python几天前,我一直在使用Grok Learning。对于挑战,我尽可能地工作,但是当我提交它时,我被告知"测试另一个以元音开头的情况。您的提交引发了IndexError类型的异常。这发生在您提交的第8行。"我不知道如何解决这个问题,甚至不知道我做错了什么。顺便说一下,我正在制作一个程序来检查消息是否以元音开头,如果是这样的话,第一个字母是10,如果不是那么第二个字母的数字是10。

set thePath to "MATRIX:Designs:Digital:Clients:"
tell application "Finder"
    activate
    repeat
        set newClientName to text returned of (display dialog "Enter New Client Name" default answer "")
        if exists folder newClientName of folder thePath then
            display dialog "There is already a Client with that name"
        else
            make new folder at folder thePath with properties {name:newClientName}
            exit repeat
        end if
    end repeat
end tell

2 个答案:

答案 0 :(得分:0)

您只收到两个字母字符串作为输入,这意味着您无法访问msg[2],因为没有此类索引。要处理这种情况,您可以将第三个值定义为下一个:

if len(msg) > 2:
    third = msg[2]
else:
    third = None

或使用一个班轮:

third = msg[2] if len(msg) > 2 else None

答案 1 :(得分:0)

嗯,这可能意味着输入的消息长度小于3。 在尝试访问内容之前,您应该检查邮件是否足够长,否则您确实会获得IndexError

length = len(msg)
if length > 2:
    third = msg[2]