Return keeps giving me None? [duplicate] - python

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))

Related

is there difference between print and return inside a function [duplicate]

This question already has answers here:
Print vs Return in Python Function
(2 answers)
Closed 6 months ago.
I'm doing some Kaggle exercises and the question is asking me to "In the cell below, define a function called sign which takes a numerical argument and returns -1 if it's negative, 1 if it's positive, and 0 if it's 0". This was my function:
def sign(x):
if x < 0:
print('-1')
elif x > 0:
print('1')
else:
print('0')
I tested on VS Code but Kaggle saying it's "incorrect". The proper solution it's the following:
def sign(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
I would like to know what's the real difference between my function and Kaggle's?
Cheers
The difference between print and return is that when you use print, you don`t have to call your function with print, example:
def sign(x):
if x > 0:
print('123')
elif x < 0:
print('134')
else:
print('Hey')
sign(1) # That is how you call your func with no print
Otherwise, if you use returns in your code, than you have to use print when you call your fucn, example:
def sign(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
print(sign(1)) # That is how you call your func with print
Also if you need to get the result value of func, then you can assign a value to a variable, example:
def func(x=0):
x = x ** 2
return x
a = func(5)
print(a) # 25

List turns to "None", trying to make a factorizing program [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 3 years ago.
I am trying to make a program that factorized numbers. I made a definition for making prime numbers, it worked fine, but when I try the factorization it doesn't work. I think the prime list turns to None. Here's the code:
def prime_num(ulist):
list_primes = sorted(ulist)
num = list_primes[-1]
i = 0
while i < 1:
num += 1
count = 0
while i < 1:
prime = True
if count >= len(list_primes):
count -= 1
break
if num/list_primes[count] == int(num/list_primes[count]):
prime = False
break
count += 1
if prime:
return num
def factorize(num):
prime_list = [2, 3]
factors_list = []
i = 0
while i <= round(num/2):
if int(num/prime_list[i]) == num/prime_list[i]:
i = 0
factors_list = factors_list.append(prime_list[i])
num /= prime_list[i]
random_list = prime_list.append(prime_num(prime_list))
prime_list = random_list
i += 1
print(factors_list)
factorize(6)
list.append() modifies the list, then returns None
When you use factors_list = factors_list.append(prime_list[i]), you append prime_list[i], then you affect None over the changed list.

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))

Python function return None. Why? [duplicate]

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)

Why does this recursive summation function return None? [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed last year.
def recursiveadd(x,sum1):
if x > 0:
sum1 += x
recursiveadd(x-1,sum1)
else:
return sum1
print recursiveadd(100,0)
Inserting a "print sum1" after the addition shows that sum1 is being increased, so I don't understand why the function returns None. The only thing I can think of is that sum1 is somehow being reset to 0 before being returned, but I have no idea why that would be.
You need to write
def recursiveadd(x,sum1):
if x > 0:
sum1 += x
return recursiveadd(x-1,sum1)
else:
return sum1
print recursiveadd(100,0)
In [52]: def rec(x, sum = None):
if sum == None:
sum = 0
if x > 0:
sum += x
return rec(x - 1, sum)
else:
return sum
....:
In [53]: rec(100)
Out[53]: 5050

Categories

Resources