#๐Ÿ”’ Making custom loops

29 messages ยท Page 1 of 1 (latest)

haughty jacinthBOT
#

@candid flax

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

candid flax
#

It is made using recurrsion

#

we need two functions to be served in one...

#

The first one is a normal loop and secondly a for each loop

#

for the function, i used a variable that i specify and kwargs for the code

#

this is how we start it

#

in order to find if a number is given for the range, i use a key present function

#

such as this

#

here i am using the keyterm length for range

#

so here, i used a try except statement if the function that the user gives needs the index or doesnt resulting in a slightly more user friendly experience

#

_For Each

#

this is the code for for each loops

#

it takes the iterable and then returns it to the function and finally recurrs

#

and thats it

#

the function is done

#

here is how to call a numbered loop

#

and this for for each loops

#

func is the function that has the code intended to run in the for loop

#

the order can be changed around as it uses a keyword system making it slightly more user friendly

hearty nebula
#

I fail to see how this is more user friendly than ```py
for x in my_list:
func(x)

#

or ```py
for i in range(10):
func(i)

#

I think you just wanted to make a c-style for loop

#

Have you considered using a generator?

#
def universal_for(iterable):
  if isinstance(iterable, int):
    yield from range(iterable)
  else:
    yield from iterable

for i in universal_for(10):
  print(f"Index: {i}")

for item in universal_for(my_list):
  print(f"Item: {item}")
haughty jacinthBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.