+ 1
How to break out of while loop using function?
https://code.sololearn.com/cLyO2nXn3DSu/?ref=app Hi Iâm practicing building functions and hit a road block - i couldnât break out of the 2 while loops and I cannot spot whereâs my mistake? Usually when I code this way by itself it works perfectly, but when I put in function somehow it wouldnât break out the the while loop. Someone enlighten me please. Thanks.
14 Antworten
+ 7
There are only 2 lines missing to run the code. Add these 2 lines:
(keep in mind that you can't run this code in playground, because of the input behaviour of Sololearn)
result = run_again(running)
if result == False: # <-----
break # <-----
and also remove "run_test":
return run_program
Here is the code as mentioned:
https://code.sololearn.com/c8Hz4nX1VcS4/?ref=app
+ 6
????? ??????, what kind of error? could you just copy the error message? what number input and operation leads to the error. I run it on iOS and win10, and get no error. Thanks!
+ 6
????? ??????, thanks for your answer. I was only focusing the issue i mentioned, but not testing the code in depth. So yes, the code i posted still had some unsolved issues.
+ 1
ok the following is the main function in question, with the code i written. The problem with the output is that when i put ânoâ to run program again, it doesnât work - it still goes back to the while loop. How should i write the function to break out of the first while loop and the one inside the function?
#the while loop function created
def run_again(run_test):
"""ask if run the program again"""
run_program = True
while run_program:
run_not = input('Would you like to run the program again (y/n): ').lower().strip()
if run_not == 'yes' or run_not == 'y':
break
elif run_not == 'no' or run_not == 'n':
run_program = False
run_test = False
print('Thank you using Python Calculator App. Goodbye.')
else:
print('Invalid input. Please enter y/n.')
return run_program, run_test
#below is the code used
running = True
while running:
input_1 = num_input()
input_2 = num_input()
operator(input_1, input_2)
run_again(running)
+ 1
The easiest way to break out of the loop is to have a funtion set the variable
running = False
I see you are trying to make a calculator. I've also tried that when I started out with python.
Then I found this way.
https://code.sololearn.com/ck9rtCKNCAdK/?ref=app
+ 1
Aurore, Ya Ting Tan
This is python, less is more, some tips.
Only use while loops if you really have to.
Don't use a while loop inside another, thats not elegant.
Let funtions return values, and let the main loop do something with the returned value
So the minimum change to make it run
Change
def run_again()
So that it returns a value,
True or False
And in the main loop write
running = run_again()
0
kiibo ghyal
i dont understand your code, your removed the while loop in function g(). If you that, when i enter a wrong key, it wonât repeat the question of â would you like to run the program again?â.
I would like the while loop inside this g() and the while loop i written outside of this g() to stop ( with code written in g() ) when i enter ânâ or ânoâ, you get what I mean?
0
Louis
do you mean i have a create another function running = False and put the function inside runagain() function?