Home > other >  Python for help: TypeError: 'NoneType' object is not iterable
Python for help: TypeError: 'NoneType' object is not iterable

Time:09-16

# to calculate the longest back to the text string in a string
S="abcicbads"
Def create_echo (s) :
Temp, max_p, length=""," ", len (s)
For I in range (length) :
Left1, right1=I, I
Def compare (l, r) :
While l!=1 and r! [l]==length and s=s [r] :
L, r=1 l, r + 1
If l/r====- 1 length:
Return the s/l + 1: r
Temp=compare (left1 right1)
If len (temp) & gt; Len (max_p) : error # visit TypeError: 'NoneType' object is not iterable
Max_p=temp
If [I]=s=s + 1] [I:
Left2, right2=I, I + 1
Temp=compare (right2, left2)
If len (temp) & gt; Len (max_p) :
Max_p=temp
Return max_p
Print (create_echo (s))


For everyone a great god answers

CodePudding user response:

Your def compare (l, r) function exists without a return value:
Because while there are three conditions, if the first two met, but the third condition is not met, namely s [l]!=s [r], quit a while,
This time, the following if not satisfied, there is no corresponding return statement, that is to say there is no return value,
So the following temp=None, len (temp) is wrong,

In addition, to the next line, s [I]==s + 1] [I, when I=length - 1 (I started with 0), I + 1 is overflow, is out of range,

CodePudding user response:

Max_p=""
So can't len

CodePudding user response:

references on the second floor day I reply:
max_p=""
So can't len

But max_p must be initialized, if max_p="" len () is illegal, it should be how to modify

CodePudding user response:

reference 1st floor rrrr336 response:
your def compare (l, r) function has no return value:
Because while there are three conditions, if the first two met, but the third condition is not met, namely s [l]!=s [r], quit a while,
This time, the following if not satisfied, there is no corresponding return statement, that is to say there is no return value,
So the following temp=None, len (temp) is wrong,

In addition, to the next line, s [I]==s + 1] [I, when I=length - 1 (I started with 0), I + 1 is overflow, is out of range,

Know that really compare not return a value, temp to None, that is I need to determine whether a temp to None, into the next if again

CodePudding user response:

Max_p=[]
  • Related