why is the print() function prints none in this case - python-3.x

I've been working on a problem I don't understand why the recursive function step returns None?
here is the code :
import sys
def minIndx(list):
x=10
indx=0
for i in range(len(list)):
if (list[i]<x):
x=list[i]
indx=i
return indx
def steps(arr,sum1,sum2,x):
i = minIndx(arr)
del arr[i]
sum1 = sum(arr)
if (sum1 + 9*x >= sum2):
return x
else: steps(arr,sum1,sum2,x+1)
s=input()
digits1=[]
digits2=[]
for i in range(len(s)):
if (i>2):break
digits1.append(int(s[i]))
for i in range(len(s)):
if (i<3):continue
digits2.append(int(s[i]))
sumLeft = sum(digits1)
sumRight = sum(digits2)
print(steps(digits1,sumLeft,sumRight,1))
for the test case : 123456,
the step function prints None as well as the print function

It could be because you're not returning your recursive call. What is your expected output here? When I return the recursive call to steps I get a value of 2 for an input of 123456.

Related

For loop, if statement and Zip python

a= ("Kiran","Narut","Sasue"]
b= ["Kiran","Naruto","Sasuke"]
def quality_check(x,y):
for i,j in zip(x,y):
if i == j:
return "Good to go"
else:
return "wrong names"
quality_check(a, b) # Good to go
It shows unexpected result. The results are shown just for the first element of the lists.
As you are using return It will only check for the first elements.
A return statement is used to end the execution of the function, Replace this with print() will loop over all the elements.
You should use yield instead of return
a= ["Kiran", "Narut","Sasue"]
b= ["Kiran", "Naruto","Sasuke"]
def quality_check(x,y):
for i,j in zip(x,y):
if i == j:
yield "good to go"
else:
yield "wrong"
for element in quality_check(a, b):
print(element)
this will return required results
every time you yield from function it returns a iterable object
Your function quality_check() is returning after the if i == j statement in the first iteration of your for loop. That if statement is comparing "Kiran" == "Kiran" (which is True).
Try checking for only 'Wrong names' in the for loop. Then return 'Good to go' once the loop completes and no Wrong names have been found.
a = ["Kiran", "Narut","Sasue"]
b = ["Kiran", "Naruto","Sasuke"]
c = ["Kiran", "Narut","Sasue"]
def quality_check(x,y):
for i,j in zip(x,y):
if i != j:
return "Wrong names"
return "Good to go"
print(quality_check(a,b))
#> Wrong names
print(quality_check(a,c))
#> Good to go

Why is no TypeError raised when iterator function is called without iterating?

For an exercise learning Python I tried to make a float_range() iterator that imitates range() but allows for floats. I try to catch wrong numbers of arguments to raise a TypeError and wrote the function below.
def float_range(*args):
start = 0.0
step = 1.0
if len(args) == 3:
start, end, step = args
elif len(args) == 2:
start, end = args
elif len(args) == 1:
(end,) = args
else:
raise TypeError()
n = start
if start < end:
if step > 0:
while n < end:
yield n
n += step
else:
if step < 0:
while n > end:
yield n
n += step
Now, I do not understand why for n in float_range(1,2,3,4): print(n) and for n in float_range(): print(n) raise a TypeError, but float_range() and float_range(1,2,3,4) do not.
If you use yield keyword in your function the function will return a generator:
print(float_range())
<generator object float_range at 0x7fe714bc2f20>
It doesn't return error because you haven't iterated over it yet. The first time the for calls your generator object created from your function, it will run the code in your function from the beginning until it hits yield.
In your case, you don't have any parameters so TypeError exception is thrown.

Question about returning a value at the end of a recursive call

In the function fp, I am basically checking whether the difference between arguments of succesive recursive calls is less than a certain number and if it is so, I would like to return the last argument. Here is the code:
pX = []
for n in range(100):
pX.append(1/((n+1)*(n+2)))
def phi1(p):
def phi2(u):
sum = 0
for k in range(len(p)):
sum += p[k]*(u**k)
return sum
return phi2
def fp(pt):
temp = phi1(pX)(pt)
print(temp)
if(abs(pt-temp) > 0.01):
fp(temp)
else:
return temp
print(fp(0.1))
The output, without print(temp) is none, with print(temp) is:
0.5175535907956329
0.619369692490415
0.6561427816319753
0.6714277125785142
0.6781654579021761
None
Desired output is 0.678. May I know where I went wrong?
You aren't returning anything when your function recurses. Add a return to where fp is called inside fp:
def fp(pt):
temp = phi1(pX)(pt)
print(temp)
if(abs(pt-temp) > 0.01):
return fp(temp)
else:
return temp

Recursion functions for factorial

I am writing this code to get the 10! ,however, I believe I have an infinite loop in there since it keeps repeating the same error code. I am trying to find the issue but can not seem to.
def calculatingfactor(num2cal):
"""this fuction will be calculating a mathematical factorial"""
if num2cal == 1:
returnvalue = 1
elif num2cal <= 0:
returnvalue = 0
else:
print("Calculating the facterial of {}".format(num2cal))
variable2 = calculatingfactor(num2cal - 1)
returnvalue = calculatingfactor(num2cal*variable2)
return
#main code
first_fact=calculatingfactor(10)
print (first_fact)
The recursive case of your code looks incorrect to me. You should be calling the same function with num2cal decremented by one, then returning the current value multiplied by whatever that recursive call returned.
def calculatingfactor(num2cal):
if num2cal == 1:
return 1
elif num2cal <= 0:
return 0
else:
print("Calculating the facterial of {}".format(num2cal))
variable2 = calculatingfactor(num2cal - 1)
return num2cal*variable2
# main code
first_fact=calculatingfactor(10)
print (first_fact)

Function can't be called

When I am running this program, then it is printing "Calling function" but the function call after that is not working. When I am calling the function in Idle, then it is working fine. Where is the mistake?
def find(word, letter):
index = 0
while index < len(word):
if word[index] == letter:
return index
index = index + 1
return -1
print('Calling function')
find('banana', 'a')
The function is returning some value.
Try this:
def find(word, letter):
index = []
i = 0
while i < len(word):
if word[i] == letter:
index.append(i)
i = i + 1
return index
print('Calling function')
print find('banana', 'a')

Resources