Passing a list from one function to another - python-3.x

I need help understanding this instruction.
Your main program code should call the “getData” function. Pass the list returned from the “summer” function to “getData” and save the sum that function returns.
def getData():
fin = open("sample.dat","r")
numbers=[]
for line in fin:
val =line.rstrip()
numbers.append(val)
return numbers
def summer(lst):
sum=0
for n in range(0,13,2):
sum+=powerval(lst[n],lst[n+1])
return sum

If the instruction read,
Pass the list
returned by the getData function to summer and save the sum that
function returns.
then try:
total = summer(getData())

I think this satisfies the requirements.
def getData(lst):
for l in lst:
do something with sum
save sum
return sum
invoke as:
getData(summer(some_other_data))

Related

Call an input list in a function from another function

this time I'm trying to figure out how can i call a function named listInput that the user will input numbers separated by commas and convert its into a list, the thing is i want instead this list is created call this function in another function that takes as args a list, how can i do it? Thanks for the answers.
def divisibleBy(lista,n):
return [x for x in lista if x % n == 0]
def inputList():
cad = input("Insert a number")
user_cad = cad.split(",")
for i in range(len(user_cad)):
user_cad[i] = int(user_cad[i])
return user_cad
print(divisibleBy(inputList(),4))
Update: Just noticed why lol, fixed code.

Program not breaking

Could anyone provide insight into why my program keeps terminating when I am trying to break?
Below is the prompt:
Write a function that returns a list of numbers, say make_list. make_list will not take any arguments. In the function body, construct an empty list and then prompt the user to enter a nonnegative number, -999 to quit. You will need a loop. If the user enters a number other than -999, add it to the list. Once the user enters -999, quit the loop and return the list. (Do not include -999 in the list.)
Write another function, say sum_list, that takes a list of numbers as an argument and returns the sum of the list of numbers. If the list is empty return 0. Do not use the built-in sum function. Use a loop.
You need to call both functions. First call the make_list function. Then use the returned list as the argument for the sum_list function. Print the result.
My solution:
def make_list():
i=[]
while(True):
n=int(input("Enter a number (-999 to quit): "))
if n==-999:
break
i+=[n]
return i
def sum_list(i):
if len(1)==0:
return 0
sum1=0
for k in i:
sum1+=k
return sum1
i=make_list()
s=sum_list(i)
print("Sum :", s)

Is there a way to get give one variable to a function and retrieve a different variable back from the second function and use it in the first function

I am trying to use the output of one function in a second function then retrieving a variable from the second function to be used in the first function. The code below is a simplification of what I am trying to do
def function1():
x=15
return x
function2(y)
print(x+y)
def function2():
y=x-12
return y
function1()
I am not getting an actual value for y when I check by adding a print statement for x in function2. Is there a way to do this or do i need to make a 3rd function to handle this?
Pass variable x to function2, and store the return value from function2 in a local variable in function 1.
def function1():
x=15
y = function2(x)
print(x+y)
return x
def function2(x):
y=x-12
return y
function1()

Need help writing a function which returns a dictionary with the keys as recursive digit sums

So I have written a function which calculates the sum of the digits when a number is input to the function. Now I am trying to write another function which would return a dictionary with the values from my digitsum function as the keys and the values would be how many times the count of that specific digitsum has occurred. Any ideas on how to go about writing the second function?
def digitsum(x):
if x < 10:
return x
else:
return (x%10) + digitsum(x//10)
def digitsumdictionary(lnum=0, hnum=100):
L =[digitsum(num) for num in range(100)]
counter = Counter(L).items()
return counter
Digitsum function is called depending on the length of the number.
You can simply find it by using len(list(str(num))). But if you want to count as the function calls itself, Then try this,
def digitsum(x, count=1):
if x < 10:
return { x : count }
else:
return {(x%10) + int(list(digitsum(x//10 , count+1).keys())[0]) : int(list(digitsum(x//10 , count+1).values())[0])}
Setting the count to 1 or 0 initially, includes or excludes the first call respectively.
The below code returns a list of dictionaries of the desired output.
[digitsum(i) for i in range(10)]

Function Help Python 3.4

def areaOfRectangle (length,width):
area = length*width
sqArea = length**2
return area,sqArea
def areaOfSquare (length,):
areaOfRectangle (length,width)
return sqArea
#def radiusOfCircle (radius):
area = 3.14*(radius**2)
return area
#def volumeOfCylinder (radius,height):
volume = 3.14*(radius**2)*height
return volume
length = int(input("Input length: "))
width = int(input("Input width: "))
print()
print(areaOfRectangle (10,20))
print()
print(areaOfRectangle (24.3,6))
print()
print(areaOfRectangle (34.9,17.4))
print()
print(areaOfRectangle (length,width))
print()
print(areaOfSquare (10.3))
I need to make two functions, the first function to calculate the area of a rectangle given the length and width. The second function needs to calculate the area of a square given the length of one of its sides. The second function should call the previous function to perform the calculation. I know how to call a function within another function however I don't know how to bring a variable from the first function to the second.
I don't know how to bring a variable from the first function to the second
Typically, this is done through the use of parameters. Let's say you have a value x:
x = 0
You can pass the value to a function by inserting it into the call itself:
f(x)
Lastly, the function needs to be able to handle the parameter you give it:
def f(y): pass
A working example of what you describe:
def f2(y):
return y + 1
def f1():
x = 2
print(f2(x))
f1()
3 is printed.

Resources