我在edx python课程中被分配来创建一个程序,该程序从给定字符串中按字母顺序打印出最长的子字符串。我已经编写了代码,但是当我运行它时,出现了“错误:检查模块中的内部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.
谢谢!
看起来大多数代码都起作用,但是,当您调用break时,它只会跳出else块,并继续运行while,其start的值大于s的最大索引。
尝试将此代码放入函数中,并在找到正确的子字符串时使用返回值
祝你好运!
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)
哎呀,试试看这个尺寸。
如果我导入任何软件包,那么我得到ERROR:root:检查模块中的内部Python错误。
我该如何解决。请帮助我。