错误:检查模块

时间:2015-06-22 13:13:04

标签: python python-2.7

我在edx python课程中被分配来创建一个程序,该程序打印出从给定字符串开始按字母顺序排列的最长子字符串。我编写了我的代码,但是当我运行它时,我得到了#34;错误:检查模块中的内部Python错误。"。我不明白为什么。如果有人能帮助我搞清楚它会很棒。这是代码:

s = 'azcbobobegghakl'
start=0
temp=0
while start<len(s):
    initial=start
    while True:
        if ord(s[start])<=ord(s[start+1]):
            start+=1
        else:
            start+=1
            if len(s[initial:start])>temp:
                sub=s[initial:start]
                temp=len(sub)
            break    
print sub

这是完整的错误:

Traceback (most recent call last):
  File "C:\Users\Yoav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\site-packages\IPython\core\ultratb.py", line 776, in structured_traceback
    records = _fixed_getinnerframes(etb, context, tb_offset)
  File "C:\Users\Yoav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\site-packages\IPython\core\ultratb.py", line 230, in wrapped
    return f(*args, **kwargs)
  File "C:\Users\Yoav\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\site-packages\IPython\core\ultratb.py", line 267, in _fixed_getinnerframes
    if rname == '<ipython console>' or rname.endswith('<string>'):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 3: ordinal not in range(128)
ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.


Unfortunately, your original traceback can not be constructed.

谢谢!

1 个答案:

答案 0 :(得分:1)

看起来代码大部分都有效,但是当你调用break时,它只会中断else块,并继续运行while,其start值大于s的max索引。

尝试将此代码放在函数中,并在找到正确的子字符串时使用return

祝你好运!

def sub_finder(s):

start=0
temp=0
while start<len(s):
    initial=start
    while True:
        if (start < len(s) - 1):
            if ord(s[start])<=ord(s[start+1]):
                start+=1
            else:
                start+=1
                if len(s[initial:start])>temp:
                    sub=s[initial:start]
                    temp=len(sub)
                break
        else:
            start+=1
            if len(s[initial:start])>temp:
                sub=s[initial:start]
                temp=len(sub)
            return sub

test = 'abcdaabcdefgaaaaaaaaaaaaaaaaaaaaaaaaaaaabbcdefg'
print sub_finder(test)

哎呀,试试这个大小。