Python function return None. Why? [duplicate] - python

This question already has answers here:
Python function returns None (all trivial solutions checked and they do not work) [duplicate]
(1 answer)
Weird function return value? [duplicate]
(3 answers)
Closed 9 years ago.
count = []
def problem14(n):
count.append(n)
if n == 1:
return count
if n % 2 == 0:
n = n/2
problem14(n)
else:
n = 3*n + 1
problem14(n)
print problem14(13)
So this is code that I have written. I have no idea why it's returning None while in my opinion it should return list 'count'. Any help?

You still need a return statement when using recursion, otherwise the return value will be lost:
def problem14(n):
count.append(n)
if n == 1:
return count
if n % 2 == 0:
n = n/2
return problem14(n) # <--
else:
n = 3*n + 1
return problem14(n) # <--
By the way, this is probably the wrong approach for Project Euler #14 :-) Consider using a dynamic programming approach instead (that's all I'll say so as to not ruin the fun).

You should use the return keyword in order to return a value from a function.
return problem14(n)

Related

Function that converts a decimal to binary: how to return result instead of printing it? [duplicate]

This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Why does my recursive function return None?
(4 answers)
Closed 6 months ago.
I need to write a function that converts an integer into binary.
The function definition I have written so far is:
def decimalToBinary(number):
if number > 1:
decimalToBinary(number//2)
print(number % 2, end = '')
My problem is that:
I should return the result instead of printing it, and I am unsure how to do that.
I am overall unsure if my code is the best recursive code I could have written.
Thank you for your time!
Convert the digit to a string, and use return instead of print. Then concatenate it with the recursive call.
def decimalToBinary(number):
if number > 1:
return decimalToBinary(number//2) + str(number % 2)
else:
return str(number % 2)
You can pass a count variable to position the digits
def decimalToBinary(number, count = 0):
if number > 1:
return ((number%2) * (10**count) + decimalToBinary(number//2, count+1))
return ((number%2) * (10**count))
Also, if you need a non-recursive function you can use something like this
def LinealDecimalToBinary(num):
count = 0
binNum = 0
while(num > 0):
binNum += (num%2) * (10**count)
num = num//2
count += 1
return binNum

Don't know how to return the final result of a recursion loop [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
I was solving a problem on Codewars involving recursion and I've never really done it before but kinda understand the concept. Im not really good at math stuff so it probably explains why I cant wrap my head around this. Essentially I think I kinda did the loop right I just dont know how to print the final value. Anyway heres my code:
def digital_root(n):
newn = 0
list = [int(x) for x in str(n)]
if len(list) >= 2:
for i in range(len(list)):
newn += list[i]
digital_root(newn)
return n
print(digital_root(1234)
output:
1234
def digital_root(n):
newn = 0
list = [int(x) for x in str(n)]
if len(list) >= 2:
for i in range(len(list)):
newn += list[i]
return digital_root(newn) # You need to return it over here
return n
print(digital_root(1234))

Return keeps giving me None? [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
Trying to build a recursive def that will counts how many times the number '7' occurs in a given number. I dont understand why my return keeps giving me None.
def count7(N):
'''
N: a non-negative integer
'''
x = 0
def helpcount7(N,x):
Ld = N % 10
NewN = N // 10
if Ld == 7:
x +=1
if NewN != 0:
helpcount7(NewN,x)
else:
print(x)
return helpcount7(N,x)
print(count7(717))
for example, if I input 717 my answer would be:
2
None
The reason is because you are not returning anything from your helpcount7() function. You should return something from it, for example:
def count7(N):
'''
N: a non-negative integer
'''
x = 0
def helpcount7(N,x):
Ld = N % 10
NewN = N // 10
if Ld == 7:
x +=1
if NewN != 0:
return helpcount7(NewN,x)
else:
return x
return helpcount7(N,x)
print(count7(717))

Why is this simple code returning `none`? [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 4 years ago.
I'm trying to do this codefights task:
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
So here's my code:
def almostIncreasingSequence(sequence):
def checa(sequence):
cont = 0
copia1 = copia2 = sequence
for i in range(len(sequence)-1):
while(cont == 0):
if(sequence[i] >= sequence[i+1]):
del(copia1[i])
del(copia2[i+1])
cont += 1
if(all(copia1[j] < copia1[j+1] for j in range(len(copia1)-1)) == True):
return True
elif(all(copia2[j] < copia2[j+1] for j in range(len(copia2)-1)) == True):
return True
else:
return False
I can't see my flaws here and it's returning None every single time.
You did not include a return statement, thus your function will always return None

Output of the Python program is coming as 'None' [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 4 years ago.
def fact(n, summ):
if n == 0:
print(summ) -- Prints 55
return summ
fact(n-1, summ + n)
print(fact(10, 0)) -- Output None
You need to return fact(n-1, summ + n) as a returning value. If a function does not return a value then it defaults to returning None.
def fact(n, summ):
if n == 0:
return summ
return fact(n-1, summ + n)
print(fact(10, 0))
This outputs:
55
On a side note, your fact function could be re-implemented without the second parameter. The following produces the same output:
def fact(n):
if n == 0:
return 0
return n + fact(n-1)
print(fact(10))

Categories

Resources