Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
def my_tup(*args):
for _ in args:
return _
print(my_tup(1,2,3,8,6))
I'm getting 1 as my output
Just as a list, I want to know if I can iterate the *args.
My expected output was (1,2,3,8,6).
The return statement in the body of the loop terminates the successful iteration early.
def my_tup(*args):
for _ in args:
print(_)
my_tup(1,2,3,8,6)
produces
1
2
3
8
6
as output.
It's not clear what my_tup is supposed to do; you could simply write
def my_tup(*args):
return args
because args is already a value of type tuple.
It does support iterating through *args argument. See for yourself. Defining
def f(*args):
for arg in args:
print(arg)
which implies iteration, is assuming, e.g. the following usage
>>> f('a', 'b')
a
b
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
#factorial prog
def fac(y):
fact=1
i=1
for i in range(1,y+1):
fact=i*fact
return fact
x=int(input("enter no"))
y=fac(x)
print(y)
why does the return function return 1
when I remove the defined function and find the factorial by same method in the main function its giving the correct answer and even in the defined function when i print fact value is providing the correct ans but only the return function is not working
can someone give me the same method code instead of recursive function
Indentation level of your return statement is wrong. You should do as following:
#factorial prog
def fac(y):
fact=1
i=1
for i in range(1,y+1):
fact=i*fact
return fact
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Passing an argument-provided value to a defined function seems impossible in my code. What am I doing wrong?
def setParams(params):
retList=[]
for param in params:
retList.append(dict(actualParameterName=param,value=params[param]))
return dict(parameters=dict(actualParameter=retList))
def runProcedure(procedure, project, invalidlyValidArguments):
print(cmdr.httpPost(cmdr.makeEnvelope(
cmdr.createRequest('runProcedure',
dict(procedureName=procedure,
projectName=project,
setParams(params=invalidlyValidArguments)
)))))
#Intended_Purpose=runProcedure(procedure='Build_Single_Module_For_Every_Config',project='My-module',validInvalidArgs={'moduleSelection':'Some_Handler','hakuna':'mattata'})
I expected this to evaluate cleanly, however I get
File "<ipython-input-34-c27d4a9b1a82>", line 19
setParams(params=invalidlyValidArguments)
SyntaxError: non-keyword arg after keyword arg
Even when I set line 19 to setParams(invalidlyValidArguments), that yields the same error.
I failed to realize that my prior call to dict() already required a key-value pair. Python was not, as I assumed upset with my function call, but rather the fact that it could not interpret the output of the setParams into a key/value pair.
setParams(params=invalidlyValidArguments)
does not evaluate to a valid key pair for the underlying dict() call to evaluate.
I need to change
dict(procedureName=procedure,
projectName=project,
setParams(params=invalidlyValidArguments)#this is NOT a valid key-pair
)
To something like
dict(procedureName=procedure,
projectName=project,
parameters=setParams(invalidlyValidArguments)
and have that setparams function remove one of its dictionary envelopes.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to update a clients details with a function. But it doesn't work. I debug the program and I realized that it never goes into the if. I don't know why?
This is the function:
def UpdateClient(self,id,cnp,name,adress):
for i in range (len(self.clients.get_all())):
if self.clients.get_all()[i].get_id==id:
self.clients.get_all()[i].set_name(name)
self.clients.get_all()[i].set_cnp(cnp)
self.clients.get_all()[i].set_adress(adress)
When I try to update the client, I give the id, name, cnp, and adress, but when I print the list nothing is changed. I cannot go into the if with the debugger because it said that they are never equal. Why?
Note that get_id is not the same as get_id(). The former references the method, while the latter actually calls it.
In addition, consider simplifying your code by writing more idiomatic Python. For example:
for client in self.clients.get_all():
if client.get_id() == id:
client.set_name(name)
...
The issue here is you are making your check as get_id == id but it is a function. Your if should be like:
if self.clients.get_all()[i].get_id()==id:
# ^ making it a function call
Also, better way to do this is by storing self.clients.get_all() in a separate variable. Hence, your code should look like:
def UpdateClient(self,id,cnp,name,adress):
clients = self.clients.get_all()
for i in range (len(clients)):
if clients[i].get_id()==id:
clients[i].set_name(name)
clients[i].set_cnp(cnp)
clients[i].set_adress(adress)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to make a function call with a dictionary like:
valueattime(simulaton_01['Temperature_05'], 20)
but I always get a syntax error with the following function
def valueattime(experiment[key], time):
...
It works with simple parameters. But for autocompletion sake it would be great to pass the parameters in the dictionary-form.
Any ideas?
You don't need to change the function signature to pass parameters directly from the dictionary:
def valueattime(temperature, time):
...
valueattime(simulation_01['temp_05'], 20) # this works just fine
Python will first run simulation_01['temp_05'] to retrieve the value, and pass it as temperature to the function.
You should pass it as a regular argument:
def valueattime(temp, time):
pass
a = {'some_key': 'some_value'}
valueattime(a['some_key], 20)
This works!
To supply dictionary items to a function, you can use dictionary comprehension:
new_dict = {k: valueattime(v) for k, v in a.iteritems()}
Remember it'a all objects...
def valueattime(temp, time)
Defines a method taking two input parameters, two object which are referred to by the names temp and time
simulation_01['temp_05'] return an object, so calling your method like: valueattime( simulation_01['temp_05'], 20 ) should be what you are looking for
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#!/usr/bin/python
def maker(n):
def action(x):
return x*n
return action
f=maker(2)
print f(3)
I have problem here with type error.How to solve this two arguments problem,one is fixed.
You need to put the return action out of action function :
def maker(n):
def action(x):
return x*n
return action
f=maker(2)
print f(3)
Result:
6
Note that in factory function you must return the inner function as the result of main function.