what is wrong with my Sum of Digits recursive function? [duplicate] - python-3.x

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 5 months ago.
def digital_root(n):
if n > 0:
a.append(n%10)
if n/10 > 0:
digital_root(n/10)
else:
if len(a) > 1:
b = a
a.clear()
z = 0
for i in range(len(b)):
z += b[i]
digital_root(z)
else:
return a[0]
why it returns None?
task is: Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer.

You got None because you seem to miss return statements when you call digital_root(). Should be:
return digital_root(n/10)
and
return digital_root(z)

Related

Binary Search function returning None value in python [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
Below is the code which I used for binary search in python using recursion. I have not used num == l[mid] since this is being handled by function calling this binarySearch. I just have to return the last item of list being left while splitting it.
def binarySearch(l,num):
length = len(l)
if length==1:
print(l)
return l
if length % 2 == 0:
mid = (length//2) - 1
else:
mid = length//2
if num < l[mid]:
binarySearch(l[mid+1:],num)
else:
binarySearch(l[0:mid+1],num)
print(l) prints the correct value which I want to return but return l gives me None instead of l
if num < l[mid]: binarySearch(l[mid+1:],num) doesn't return anything: it calls binarySearch and discards its return value. Thus, None is returned implicitly. You should actually return the values you want to be returned:
if num < l[mid]:
return binarySearch(l[mid+1:],num)
else:
return binarySearch(l[0:mid+1],num)
Here, return binarySearch(...) means "return whatever the call to binarySearch returned".

Python 3 : List of odd numbers [duplicate]

This question already has answers here:
Python "for i in" + variable
(3 answers)
Closed 2 years ago.
I'm trying to return a list of odd numbers below 15 by using a python user-defined function
def oddnos(n):
mylist = []
for num in n:
if num % 2 != 0:
mylist.append(num)
return mylist
print(oddnos(15))
But I'm getting this error :
TypeError: 'int' object is not iterable
I didn't understand what exactly this means, please help me find my mistake
Because 15 is an integer, not a list you need to send the list as an input something like range(0,15) which will give all numbers between 0 and 15.
def oddnos(n):
mylist = []
for num in n:
if num % 2 != 0:
mylist.append(num)
return mylist
print(oddnos(range(0,15)))
When you're passing values to the function oddnos, you're not passing a list of values till 15, rather only number 15. So the error tells you, you're passing an int and not a list, hence not iterable.
Try to use range() function directly in the for loop, pass your number limit to the oddnos function.

Return statement is not working in python inside if condition [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
def digital_root(n):
s=0
if n < 10:
return n
else:
while n>0:
s+=n%10
n=n//10
digital_root(s)
I am having trouble submitting this question.
I made sure to return the digit and i checked that the digit was correct by outputting it to the logs, but the tests keep failing and saying that I am returning None. I am not.
You need a return before the recursive call:
def digital_root(n):
s=0
if n < 10:
return n
else:
while n>0:
s+=n%10
n=n//10
return digital_root(s) # added 'return' here

Python 3, any better/cleaner way to write these functions that use for loops?

I'm trying to write code in the most simplest and cleanest way possible. I've found a few ways to shorten and simplify my code through functions that I've never seen before or through using other methods. I'd like to expand my knowledge on writing code using various (but simple) methods, and also expand my function 'vocabulary'.
Here are the functions:
1. Perfect number:
If a number's divisors' sum is equal to the number itself, it is a perfect number. We dont count the number itself as a divisor. E.g. 6's divisors are 1, 2, 3. The sum of the divisors is 6. Therefore 6 is a perfect number.
def perfect_number(num):
if type(num) != int or num < 0:
return None
divisors = []
total = 0
for x in range(num):
if num % (x+1) == 0:
if num != x+1:
divisors += [x+1]
for x in divisors:
total += x
if total == num:
return True
return False
2. Pattern:
A function that takes a positive integer and prints a pattern as follows:
pattern(1): '#-'
pattern(2): '#-#--'
pattern(5): '#-#--#---#----#-----'
def pattern(num):
if type(num) != int or num < 0:
return None
output = ''
for x in range(num):
output += '#'+('-'*(x+1))
return output
3. Reversed Numbers:
A function that takes 2 integers. It goes through every number in the range between those 2 numbers, if one of those numbers is a palindrome (the same thing backwards e.g. 151 is a 'palindrome'), it will increase a variable by 1. That variable is then returned.
invert_number(num) returns the opposite of num as an integer.
def reversed_numbers(low, high):
output = 0
for x in range(low,high+1):
if invert_number(x) == x:
output += 1
return output
It is assumed that low is lower than high.
If I broke a rule or if this doesnt fit here, please tell me where I can post it/how I can improve. Thanks :)

check if only one of the numbers is odd or even from user input list [duplicate]

This question already has answers here:
How to compare multiple variables to the same value?
(7 answers)
Closed 5 years ago.
I am learning python and struggling with this so please help. out of 4 different user input integers, I want to print 'False' if there's a single odd or an even integer in the list. say if user inputs 1,1,2,2, = true...
but 1,1,1,2, or 1,2,2,2 = false
my attempt was to check if only one in the list divisible by two (or not) to return false.
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if a or b or c or d % 2 == 0:
print ('FALSE')
elif a or b or c or d % 2 != 0:
print('FALSE')
else:
print('TRUE')
please help a guide to clean my mess or understanding .. thank you!
You're effectively testing whether there's an odd number of evens among four numbers. If there's an odd number of evens, then the sum of the four values will be odd. You can therefore check the sum as following:
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if (a + b + c + d) % 2 == 0:
print ('TRUE')
else:
print('FALSE')

Resources