Call a function in it self? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I saw an example online that looks like this:
def DecimalToBinary(num):
if num >= 1:
DecimalToBinary(num // 2)
print(num % 2, end = '')
print(DecimalToBinary(12))
I'll be happy if someone explain it to me.

This is called recursion. There is no problem as long as there is a break condition, in other words the calls to itself stop at some point. Otherwise you have an infinite loop.

The function will run normally as if you called it outside of it

It is called recursion. A type of concept of function calling itself. If it has no proper if statement that terminates the function itself, loop will not end or it will error. In your case it terminated.
01100None

Related

Is indirect recursion possible in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I tried searching online but did not find any answer to this particular question. In python there are no function declarations and a function can't be used until it has been defined.
Does this mean that indirect recursion is impossible in python?
And is there a way around this by using some modules?
No, it is possible
def f():
print('from f')
g()
def g():
print('from g')
f()
"a function can't be used until it has been defined" is not so straightforward. When the code runs, the name of the objects that it refers to have to exist. So, you can't do
f()
def f():...
because f() actually executes something. But definitions create a function object, without running it at the time. In the example, the function is claled at the last line of the script, and, by that time, both f, g do exist.

How to pass a 2-dimensional list to a function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
this is my first post.
The Problem:
I have 2-Dimensional-list which is decleared and filled in a function - looks like this: data = [[1, 2, 3, 4], [10, 20, 30, 40]] - Now I want to pass that list from this function to another to continue working with the list.
I think that you're wanting to do something like this:
def myfunction():
data = [[1,2,3,4],[10,20,30,40]]
return data
def function2(myList):
# do stuff with it here
myList = myfunction()
function2(myList)
But your question is not very clear.
I think it's not an issue with python. Python allows You to pass multidimension lists to functions.
There shouldn't be any issue with this.
just pass like a simple variable.
I think I know your problem, it is the same process to pass a multidimensional array to a function as any other variable, the problem is other code, python is really annoying with tabs and spaces and it can cause confusing errors aside from the usual inconsistent use of tabs and spaces error. Just try deleting the function that's not working and rewrite it and you'll probably fix your error

Not Match Between Argument Passed and List Available [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I was working with Python3, and trying some sorts of code. And then i came to try some function features, here is my code
def print_list_members(some_list):
for i in first_list:
print(i)
that's all my function definition. and then i add for example new to the code
first_list = ["Alfried", "Michael", "John"]
second_list = ["Joseph", "Tim", "Delta"]
then i try to produce traceback by passing different argument with the function code
print_list_members(second_list)
but, no traceback raised, except something make me a bit confused, the output is
Alfried
Michael
John
the question is, how it be possible? or is it an error from python itself?
Change your code here
def print_list_members(some_list):
for i in some_list:
print(i)
You iterate over the global first_list inside the body of the function, so you print first_list. Whatever you pass as an argument is ignored. Perhaps you wanted iterate over some_list?

Re-usable function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)

Python - Disable nested for loop according to boolean value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
What is the best way to rewrite nested for-loops like this:
for i in my_generator_function(args...):
#do stuff...
for j in another_generator_function(args...):
#do more stuff
if i want to disable the first for-loop, according to a boolan value, but always keep the inner one? Of course I could use an if/else around it, and put a duplicate of the inner loop in the else block. but is there a better way?
If I understood correctly you want to loop 100 or 0 times through something and keep the inner 10.
If that is what you want, try this:
my_value = False
checker = lambda x, y: y if x else range(1)
def somefunc():
return range(100)
for i in checker(my_value, somefunc()):
if my_value:
#do stuff
for j in range(10):
#do stuff

Categories