Unable to print output from a python function [duplicate] - python

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 7 years ago.
I made a simple function to identify the tennis player who served the last point in a tie-breaker match. Below is my code, which returns the player but when I am printing the result, I get None.
def get_tie_break_point_server(s,tie_break_sum,playerA,playerB):
s = s+1
if(s == tie_break_sum):
return playerB
s = s+1
if(s == tie_break_sum):
return playerB
else :
get_tie_break_point_server(s,tie_break_sum,playerB,playerA)
tie_break_score = [2, 2]
playerA = "A"
playerB = "B"
tie_break_sum = tie_break_score[0] + tie_break_score[1]
print get_tie_break_point_server(1,tie_break_sum,playerA,playerB)

Your function returns None when it reaches the last 'else' block. When a function reaches the end of its code block without an explicit return statement it will implicitly return None. You need to change the last line to
return get_tie_break_point_server(s,tie_break_sum,playerB,playerA)

Related

return True for recursive function in python [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 1 year ago.
could someone please indicate to me what's wrong with the logic of the simple recursive function, below:
def palindrome (string):
if len(string)<=1:
return True
elif string[0]== string[-1]:
palindrome(string[1:-1])
else:
return False
the False part, works ok, for example :
palindrome('good') the function returns correctly False.
but if it's fed a palindrome like :
palindrome('level') the function returns nothing.
As mentioned in the comments, you don't return the function when calling it recursively, but you should, or the original function call will return None.
Solution:
def palindrome (string):
if len(string)<=1:
return True
elif string[0].lower() == string[-1].lower():
return palindrome(string[1:-1])
else:
return False
I would also add "lower" as capitalization isn't relevant

Why do I keep getting the output I want and an unwanted "none" [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 6 months ago.
def car(wheels,bodies,figures):
car_by_wheels=wheels//4
car_by_figures=figures/2
if figures//2 >= car_by_wheels and bodies >= car_by_wheels:
print(car_by_wheels)
elif car_by_wheels >= car_by_figures and bodies >= car_by_figures:
print(car_by_figures)
elif car_by_wheels >= bodies and car_by_figures >= bodies:
print(bodies)
else:
print("0")
print(car(3,29,54))
I tried a few other examples and the code works fine but I keep getting None. Why?
Your function doesn't explicitly return anything, so it implicitly returns None. So car(3,29,54) does it's printing and returns None, which you then print.

IndentationError: unindent does not match any outer indentation level (Elif) [duplicate]

This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 2 years ago.
def gcd(a,b):
if b == 0:
return a
elif a < b:
return gcd(b,a)
else:
return gcd(b,a%b)
I am new to python, i hope this has something to do with spaces.
Can anyone help me out.enter image description here
Also attached the image. Thanks
If you read the error, it says IndentationError, so you have problem with the indentation. You should know that python depends on indentation to define the scope of each block, so the content of a function must be indented, otherwise it won't be considered as the content of the function.
def gcd(a,b):
if b == 0:
return a
elif a < b:
return gcd(b,a)
else:
return gcd(b,a%b)
Function definitions also have a level of indentation to them, so it should look as follows:
def gcd(a,b):
if b == 0:
return a
elif a < b:
return gcd(b,a)
else:
return gcd(b,a%b)

Python - Recursive Function Without Return Logic [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 4 years ago.
I had trouble understanding why this function returns None:
def rem(x, a):
"""
x: a non-negative integer argument
a: a positive integer argument
returns: integer, the remainder when x is divided by a.
"""
if x == a:
return 0
elif x < a:
return x
else:
rem(x-a, a)
rem(7, 5)
I then realized that the last line in the function should be:
return rem(x-a, a)
I am still unsure as to what really happens in the first one. It looks like there are 2 seperate function calls, one returning 2 and the other None... any help?
All python function returns None if it doesn't terminate by a return statement. In your case, you successfully called the recursion but you discarded the result in the else part. And after the if-block, the default return None is implicitly executed.

Find Max number Recursive function return None? [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 7 months ago.
I tried to write a program to return he max number, however it returns none. I look into similar questions but I did't find the answer.
def findMax(alist):
if len(alist) == 1:
return alist[0]
else:
if alist[0] > alist[1]:
alist[1] = alist[0]
findMax(alist[1:])
def main():
a = [1,3,4,2,6,7,9,12,3,20,4,32,5,6,9,10]
print(findMax(a))
main()
Recursive functions need to return something under all conditions, you don't return anything under the else condition. Try return findMax(alist[1:]).

Categories

Resources