How come my function didn't print a output? [Python] [duplicate] - python

This question already has an answer here:
Python function returns None, unclear why
(1 answer)
Closed 2 years ago.
I created a function to square some numbers. I was wondering why my code didn't print.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
A = [2,0,-3]
print(Square(A))

Add a return value.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
return A
print(Square(A))

Related

Python return function not giving value [duplicate]

This question already has an answer here:
Python script run through IDLE has no output
(1 answer)
Closed 2 years ago.
In python code, I've written:
x = 1
y = 2
def add_nums(first, second):
sum = first + second
return sum
And when I do:
add_nums(x, y)
It returns nothing what is wrong with the code?
The function returns its local sum variable. Perhaps you meant to print it
print(add_nums(x, y))

Is returning a list a good programming practice? [duplicate]

This question already has answers here:
What's the best way to return multiple values from a function? [duplicate]
(6 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 2 years ago.
So i have a function that has to return 3 values, i haven't found a better way to do this other than returning a list. Is this code a good programming practice? And if not how to fix it.
Example function:
def func():
#code
return [a,b,c]
Main code:
#code
list = func()
k = list[0]
l = list[1]
m = list[2]
You can pack/unpack directly in python:
def func():
a = 1
b = 2
c = 3
return a, b, c
k, l, m = func()

For loop + Booleans [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 3 years ago.
Given this code:
nums = [1,2,3]
def test1(nums):
for i in nums:
if i == 1:
return True
return False
test1(nums)
The result is True even though after the for loop there is a return False. What am I missing?

Can anyone please explain how does this function work? [duplicate]

This question already has answers here:
Understanding factorial recursion [duplicate]
(3 answers)
Closed 5 years ago.
I'm having trouble figuring out how does this exactly work. Can anyone please explain?
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(raw_input())
print fact(x)
It gets the factorial of the value x. The value of x is whatever you enter since x = int(raw_input) gets the user input. For example if x=3, then fact(3) will be 3×2=6....

Why python recursion return None Value [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 6 years ago.
I'm trying to solve some codility task using recursion in Python.
Can someone check this code and tell me why it returns None? I want to get [4, 5] in the variable called "solution".
def rec_fun(upstream,downstream):
if not upstream or not downstream :
if upstream :
return upstream
if downstream:
return downstream
if upstream[0] >downstream[0] :
downstream.pop(0)
else:
upstream.pop(0)
rec_fun(upstream,downstream)
def solution(A, B):
upstream=[]
downstream=[]
n=len(A)
for i in range(0,n) :
if B[i]==0:
upstream.append(A[i])
else:
downstream.append(A[i])
upstream=sorted(upstream)
downstream=sorted(downstream)
return rec_fun(upstream,downstream)
A=[4,3,2,1,5]
B=[0,1,0,0,0]
solution = solution(A, B)
print solution
The output is: output = None, it should be [4, 5].
In your recursive function you are not returning anything. You have to add some return statement. Namely:
return rec_fun(upstream,downstream)

Categories

Resources