+ 1
How to print all conditions in a loop?
What loop do you think will perfectly print all possible conditions. guess that I want to print all conditions one by one or apply all conditions. please check below codes and offer your ideas. https://code.sololearn.com/cuDS9oYqRch5/#py Thank you all ;-)
13 ответов
+ 3
Hello
You are mixing up a lot of things and it's not clear what you want to achieve. Maybe something like this:
https://code.sololearn.com/cKf7IQDiP5mV/?ref=app
Try to design what your function really does. Ideally a function should be isolated from the "outside" code and is not supposed to know about those variables which are not passed to it directly as a parameter.
A function can have inputs (parameters) and outputs (return value). If this is a printing function then just do the print inside the def. The output is irrelevant so don't do print(my_func).
You can use a loop to cycle though the elements of a list or tuple or dictionary. In my example I gave the 4 variables as parameters to the function. So
def my_func(*args):
takes all those values in the args list. Then cycle though them with:
for x in args:
+ 3
You can get the same result with a dictionary or even a simple list as well, without any need to use function or if conditions.
https://code.sololearn.com/cpDLMP9L3c5U/?ref=app
Only with the dictionary it is not guaranteed that the values will be iterated in the same order. To make sure you can do like this:
for key, value in sorted(nums.items()):
+ 3
I would use a dictionary like Tibor Santa suggested. It's the closest thing to a switch statement that python has to offer.
Why do you think that myfunc() is not correct? If you want to check the values of different values, you can't combine the statements with elif. If i is 1, the condition is True and it won't check the elif (else + if) statements.
If you check each condition individually, each number will be printed no matter if the other conditions are True or False.
And as soon as a function returns anything, other if statements within the function won't be checked. You could rebuild your function to be a generator function, but I don't really see any advantages:
https://code.sololearn.com/c78gHuG99Fdq/?ref=app
+ 3
Dolan Hêriş You can apply several functions like this (but it doesn't really seem to be what you're looking for):
https://code.sololearn.com/cCH3zGf221fr/?ref=app
+ 3
Maybe if you can describe exactly what you want your program to do, we could suggest the ideal solution :) your original example did not have strings.
I think you use the term "function" in a different sense than what it means in Python, but I could be wrong...
+ 2
Yes, you can use return inside the function instead of all prints (just be consistent with the logic).
They you can assign the result of the function to a variable, or print it directly on the outside.
+ 1
Tibor Santa , Thanks for replying. good points. Actually it is sth like what you did. but I want to know different ways to do it.
Thanks again
+ 1
Tibor Santa
how about using ‘return’ instead of using ‘print’ in the same (for loop) and also print all of them:
https://code.sololearn.com/cKf7IQDiP5mV/?ref=app
0
This way won’t be effective for applying more than one function or condition one by one. Imagine I have more than two or three conditions and I want to apply all of them on a text. Then it won’t help like for loop which you offered.
0
But when using return, it gives no output, while I want to print all of them!
0
Anna the main purpose is to apply more than one function via a loop.
Imagine I have a text and I have three functions (to replacing some special characters) and I want to apply them one by one to the text via a loop. That’s why I said the dictionary can’t be helpful.
0
hmm! I should check it out later!
by the way, Thanks. if you think this way is not practical, I would appreciate to let me know the other ways if possible to offer.