This question already has answers here:
Return multiple values over time
(2 answers)
Closed 5 years ago.
I've written a function in python to see If i know what I'm doing but unfortunately I'm not. I thought my defined arg variable will hold a number and multiply each of the numbers which *args variable holds and print it all at a time. But, it is giving me the first result only.
This is what I've written:
def res_info(arg,*args):
for var in args:
return var*arg
print(res_info(2,70,60,50))
Having:
140
Expected:
140
120
100
I don't understand why I'm not getting all the results. Thanks for taking a look into it.
You are on the right path. The problem you had was due to your use of the return statement. Use yield instead.
>>> def res_info(arg,*args):
for var in args:
yield var*arg
>>> list(res_info(2,70,60,50))
=> [140, 120, 100]
So, what was happening was, even though your logic was correct, since there was a return statement in the loop, your loop hence was never fully executed and your program scope would come out on the first iteration of the loop itself (ie, when var = 70).
Using yield, solved the problem as it returns a Generator object after all calculations, and hence does not exit the loop like return does.
def res_info(arg,*args):
result = []
for var in args:
result.append(var*arg)
return result
print(res_info(2,70,60,50))
Related
This question already has answers here:
Getting one value from a tuple
(3 answers)
Closed 2 years ago.
variable1 = 1
variable2 = 2
a_tuple = (variable1, variable2)
def my_function(a):
pass
my_function(a_tuple(variable1))
Is there a way I can pass a specific value from a tuple into a function? This is a terrible example, but all I need to know is if I can pass variable1 from the tuple into the function, I understand in this instance I could just pass in variable 1, but its for more complicated functions that will get its data from a tuple, and I don't like the look of that many variables, too messy.
variable1 = 1
variable2 = 2
a_tuple = (variable1, variable2)
def my_function(a):
pass
my_function(*a_tuple)
This code would obviously provide an error as it unpacks the tuple and inserts 2 variables, to make this work in my program I would need a way to pass either variable1 or variable2 into the function. My question is can I define exactly which items from a tuple are passed into the function when calling the function? Latest version of Python if it matters.
P.S. I wrote print("hello world") for the first time 7 days ago, this is my first language and my first question I couldn't find an answer to. Go easy on me, and thank you for your time.
In the code you provided you don't have a tuple you have a list. But it is still pretty much the same.
In your example lets say that you wanted to pass the first variable you would do it like this:
my_function(a_tuple[0])
If you don't understand why there is a zero here and how does this work I highly suggest learning about lists before functions.
You just need to access individual elements of the tuple, using index notation:
my_function(a_tuple[0])
or
my_function(a_tuple[1])
You could, if you wanted, write a new function which takes a tuple and an index, and calls my_function with the appropriate element:
def my_other_function(tuple, index):
return my_function(tuple[index])
But I don't see how there would be much gain in doing that.
you can index a tuple or use the index method.
def my_function(a):
pass
my_function(a_tuple[0])
if you want to get the index of a value use the index() method
a_tuple.index(variable1) #this will return 0
This question already has answers here:
Python for loop returns True after first item
(4 answers)
Closed 3 years ago.
I'm trying to make a function using *args. It should take in certain numbers and then output the products of the numbers multiplied by 10.
I'm learning to use *args but I'm having trouble getting the output. My result only outputs the first number in the function. I've tried using a for loop, thinking it would iterate over each character, but it didn't work.
def myfunc(*args):
for args1 in args:
return args1*10
When I run the function:
myfunc(10.1,20)
The expected output should be
(101.0,200.0)
The actual output is
(101.0)
Any help would be appreciated. Thanks.
If I understand you, that's what you need:
def myfunc(*args):
return tuple(map(lambda x: x*10, args))
*args can be retrieved in the function as a list called args. What you do here is just multiplying the first element by 10 and return it, so the function stops.
You should use yield instead of return, to make your function return a generator, which can then be converted to tuple whenever you need
>>> def myfunc(*args):
... for args1 in args:
... yield args1*10
...
>>>
>>> myfunc(10.1,20)
<generator object myfunc at 0x7f20d1ae8ad0>
>>>
>>> tuple(myfunc(10.1,20))
(101.0, 200)
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 5 years ago.
I'm trying to create a function that uses a while loop to count up from one to a number given by a user. The code executes as I intend it to but returns None at the end. How do I get rid of the None? Here's the code.
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
print (printFunction(int(input())))
You can use this code to prevent none, tough its just the last line changed
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
printFunction(int(input()))
In the last line you were using
print(printFunction(int(input()))) which was getting you None after printing the results.
Instead just use printFunction(int(input())). This will not print None. You can also use a message to ask user like printFunction(int(input("Enter a number"))). Since there is noting getting returned you no need to use print.
This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 5 years ago.
I am having issues with values being returned exactly as they are passed to a method despite being modified inside the method.
def test(value):
value+=1
return value
value = 0
while True:
test(value)
print(value)
This stripped-down example simply returns zero every time instead of increasing integers as one might expect. Why is this, and how can I fix it?
I am not asking how the return statement works/what is does, just why my value isn't updating.
You need to assign the return'd value back
value = test(value)
Use this :-
def test(value):
value+=1
return value
value = 0
while True:
print(test(value))
You weren't using the returned value and using the test(value) inside the print statement saves you from creating another variable.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I have written the following python program:
#!/usr/bin/env python
def bug( numbers = [] ):
numbers.append( 1 )
return numbers
print bug()
print bug()
The result i would expect is
[1]
[1]
But i got
[1]
[1, 1]
Is this a bug?
No, this is not a bug and this behaviour has been around in Python for a very long time.
The problem is that the list object is mutable, i.e. you can change it, and when you call a function you don't get a new default value. What's happening is this:
def bug( numbers = [] ):
numbers.append( 1 )
return numbers
At this point the function bug has been created and the list that is default value for numbers created.
print bug()
Now we've called bug once and added 1 to the list that was created when the function was defined.
print bug()
When we call the function again we get the same list as before so we get two 1s added to the list.
The usual solution is to define your function as follows:
def bug(numbers = None):
if numbers is None:
numbers = []
numbers.append(1)
return numbers
Read this for more details.
numbers=[] is evaluated only once (when the function is defined). So it's always the same list.
To avoid this, change the function like this:
def not_a_bug(numbers=None):
if numbers is None:
numbers = []
numbers.append(1)
return numbers