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.
5/28/2020 5:35:10 PMAurore, Ya Ting Tan
14 AnswersNew Answer
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
????? ??????, 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!
????? ??????, 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.
Lothar I think you have not found the error The code is completely correct See the problem 👇👇👇 a=5 b=6 add I want to quit a=4 b=2 The loop never ends after quitting it again it asks for user input We want a=5 b=6 add I want to quit Thanks for using calculator Hope you now Understood the problem
Aurore, Ya Ting Tan Your code is too long Plz put a small example of input & output here
Aurore, Ya Ting Tan Here's your corrected code 👇👇👇 Feel free to ask if you have any doubt🤗 https://code.sololearn.com/cp5MehJynPem/?ref=app
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)
Aurore, Ya Ting Tan I got your problem, but I need time to correct it
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
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()
Lothar It's giving error
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?
Louis do you mean i have a create another function running = False and put the function inside runagain() function?