python -- how to prevent the first iteration and the second iteration to have same value on a same variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

python -- how to prevent the first iteration and the second iteration to have same value on a same variable

i have this function below def looping_steps(): print('**** Checking Balance ********') while True: balance = 20 if balance > 0: tbs = balance * 0.9 break print('repeat again and again') # if its balance bigger then zero print('do something ' ) while True: looping_steps() i want the second iteration of while loop if it equal 20 for example i want to change tbs formula for example tbs = balance * 0.8 how can i acheive

27th Jan 2024, 8:47 PM
Haji Abdirahim Ali
Haji Abdirahim Ali - avatar
5 Answers
0
i would like to give an example and you know getting balance is dynamic it will be the first iteration 10 and i want to avoid the second iteration to become 10 again if it's become i want to change the formula
27th Jan 2024, 10:03 PM
Haji Abdirahim Ali
Haji Abdirahim Ali - avatar
+ 2
Haji Abdirahim Ali , If you want a function to remember variable values between calls (and the changes you make to them), make it a generator by using yield instead of return (your function currently uses neither). Generators are taught in Python Intermediate. Don't hard code 20 inside the while loop, or it will reset to 20 each iteration.
27th Jan 2024, 9:55 PM
Rain
Rain - avatar
+ 2
Haji Abdirahim Ali , There's an indentation error in the line containing not. I want to help you clean up this part: while True: balance = 20 if balance > 1: if use_alternative_formula: tbs = balance * 0.95 else: tbs = balance * 0.92 break print('repeat again and again') You have while True: but it only executes once because you always break, so that can be removed. You set balance to 20 and immediately check if balance > 1, which is always True, so you don't need that either. Because you always break, print('repeat again and again') is never reached, so you don't need that either. And without while, we don't need break. This shorter code would produce the same behavior: balance = 20 if use_alternative_formula: tbs = balance * 0.95 else: tbs = balance * 0.92
28th Jan 2024, 12:08 AM
Rain
Rain - avatar
+ 1
Haji Abdirahim Ali , If you don't know generators yet, you can make a function use different values by sending those values as arguments and changing the values at the call site rather than in the function. def add_tax(price, tax): return price * tax tax = 1.0 for price in range(10, 40, 10): tax += 0.04 print(price, tax, add_tax(price, tax)) """ Output: 10 1.04 10.4 20 1.08 21.6 30 1.12 33.6 """
27th Jan 2024, 10:22 PM
Rain
Rain - avatar
0
@rain i got it the point, what i was need is Boolean to determine every iteration i use use_alternative_formula = False def looping_steps(): print('**** Checking Balance ********') global use_alternative_formula while True: balance = 20 if balance > 1: if use_alternative_formula: tbs = balance * 0.95 else: tbs = balance * 0.92 break print('repeat again and again') # if its balance bigger then zero print('do something ' ) # update the boolean flag to true / false use_alternative_formula = not use_alternative_formula while True: looping_steps()
27th Jan 2024, 11:37 PM
Haji Abdirahim Ali
Haji Abdirahim Ali - avatar