Home > other >  Consult the results don't return True or False
Consult the results don't return True or False

Time:11-05

Just learning python, using thinkpython2 text, one practice is about how to quickly determine whether a numeric or character in the order number or character combination, the approach is from 1/2 to find every time,

I wrote a code, as follows: the first function is to see characters in not in the list of the characters, the second search function to generate the initial position, and calls the first function, I met the problem is not running after True or False, but I really don't see where is the problem, I try to add a lot of print in the code, to see the program runs, seems to be no problem, and finally to the if start + 1==end: or elif a==t [int (+ end (start)/2)] :

Predecessors, please help me to see where the problem is, many thanks!



Def is_there (a, start, end, b) :

"' is an integer or a string, b is a list, the start and end are the range of the list for the searching '"'

T=sorted (b)

If start + 1==end:

Return (a==t [end] or a==t [start])

Elif a==t [int (+ end (start)/2)] :

Return True

Elif a> T [int (+ end (start)/2)] :

Start=int (+ end (start)/2)

Is_there (a, start, end, t)

The else:

End=int (+ end (start)/2)

Is_there (a, start, end, t)



"' the following function takes a string or an integer and a list as the argument of ' ' '

Def in_bisect (a, b) :

Start=0

End=len (b) - 1

Is_there (a, start, end, b)



B=,2,3,4,5,6,7,8,9,0,12,23,54,7523,4365,12443,43643,123645,3242456,324244 [1]

A=111

In_bisect (a, b)

CodePudding user response:

I convert the return to the print run again after, can show the True or False

CodePudding user response:

The return is not print

CodePudding user response:

This debug I doubt life, a great god answers such as mark

CodePudding user response:

But one thing is certain, your in_bisect not return a value

CodePudding user response:

Is recursive functions with no return reason,
The code is as follows:
 def is_there (a, start, end, b) : 
T=sorted (b)
If start + 1==end:
Return a [end] or a====t t [start]
Elif a==t [int (+ end (start)/2)] :
Return True
Elif b & gt; T [int (+ end (start)/2)] :
Start=int (+ end (start)/2)
Return is_there (a, start, end, t)
The else:
End=int (+ end (start)/2)
Return is_there (a, start, end, t)

Def in_bisect (a, b) :
Start=0
End=len (b) - 1
Return is_there (a, start, end, b)

B=[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 12, 23, 54, 7523, 4365, 12443, 43643, 123645, 3242456, 324244]
A=111
Print (in_bisect (a, b))
A=1
Print (in_bisect (a, b))

The result is True and False:
False
True,

CodePudding user response:

The
reference 2 floor space to the breeze full building response:
return is not print


It doesn't have to be, this is very simple code, after the operation will be output True or False
Def xc: (a)
If a % 2!=0:
Return True
The else:
Return False

Xc (4)
Xc (7)

CodePudding user response:

happy pigs reference 5 floor response:
is recursive functions with no return reason,
The code is as follows:
 def is_there (a, start, end, b) : 
T=sorted (b)
If start + 1==end:
Return a [end] or a====t t [start]
Elif a==t [int (+ end (start)/2)] :
Return True
Elif b & gt; T [int (+ end (start)/2)] :
Start=int (+ end (start)/2)
Return is_there (a, start, end, t)
The else:
End=int (+ end (start)/2)
Return is_there (a, start, end, t)

Def in_bisect (a, b) :
Start=0
End=len (b) - 1
Return is_there (a, start, end, b)

B=[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 12, 23, 54, 7523, 4365, 12443, 43643, 123645, 3242456, 324244]
A=111
Print (in_bisect (a, b))
A=1
Print (in_bisect (a, b))

The result is True and False:
False
True


Thank you very much, indeed as expected, also want to ask why must want to add the return, even if they don't add extra three return, recursive statements would eventually return to return statement, why this case is not return results

CodePudding user response:

reference 4 floor QQ114152850 response:
but one thing is certain, your in_bisect has no return value


Thank you admire, downstairs has been answered, I don't quite understand there is no return value, is_there in_bisect invoke another function, this function has a return, this case has no return value?

CodePudding user response:

I also want to ask, why not return?

If you return, the else branch need not return?
That, of course, if you never go less than the else there, is no return
  • Related