+ 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.

28th May 2020, 5:35 PM
Aurore, Ya Ting Tan
Aurore, Ya Ting Tan - avatar
14 RĂ©ponses
+ 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
28th May 2020, 5:50 PM
Lothar
Lothar - avatar
+ 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!
28th May 2020, 8:01 PM
Lothar
Lothar - avatar
+ 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.
28th May 2020, 8:16 PM
Lothar
Lothar - avatar
+ 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)
28th May 2020, 5:46 PM
Aurore, Ya Ting Tan
Aurore, Ya Ting Tan - avatar
+ 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
28th May 2020, 5:54 PM
Louis
Louis - avatar
+ 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()
29th May 2020, 3:09 AM
Louis
Louis - avatar
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?
29th May 2020, 2:24 AM
Aurore, Ya Ting Tan
Aurore, Ya Ting Tan - avatar
0
Louis do you mean i have a create another function running = False and put the function inside runagain() function?
29th May 2020, 2:31 AM
Aurore, Ya Ting Tan
Aurore, Ya Ting Tan - avatar