Note: I'm using pyinputplus
module...
Go to the elif sub_inp == 'Go Back':
line, I need to skip the rest of the code and come back to the start of the main while loop if user chooses Go Back:
choice_loop = 'yes'
while choice_loop == 'yes':
# Taking main_inp (this is choice between calculator or unit converter)
# Nested While loop for making repeated use of a single operation possible
sub_loop = 'yes'
while sub_loop == 'yes':
# here sub_inp is taken (this is choice between some different types of calculators)
# rest of my text based calculator here
elif sub_inp == 'Go Back':
# HERE IS THE PROBLEM/ISSURE, What should I do here to skip the code below to directly go back to taking main_inp above ??
# Internal While loop repeater
internal_loop = inputYesNo(f'\nDo you want to conduct {sub_inp} again ? [Yes/No]: ')
if internal_loop == 'yes':
print()
continue
else:
break
# After many lines of text based unit converter (its same as the calculator section above but just with diff operations)
# at the end this also has a internal_loop and after this whole loop ends sub_loop runs
# Sub While loop repeater
sub_loop = inputYesNo(f'\nDo you want to conduct an operation in the {main_inp} Category? [Yes/No]: ')
if sub_loop == 'yes':
print()
continue
else:
break
# Main While loop repeater:
choice_loop = inputYesNo('\nDo you want to access another Category ? [Yes/No]: ')
if choice_loop == 'yes':
continue
else:
break # Code ends here
# I need a way to skip internal and sub loop to directly go back to main loop if user chooses to go back (if they choose a option by mistake)
I've plans to learn PySide6 and make this in GUI form just to learn GUIs but currently help me resolve this please!
I tried thinking of something but no luck: quit()
, exit()
, break
, continue
. I'm not using them correctly to achieve my end goal
As Marcel said in his comment, the easiest thing to do would be to put the inner while loop in its own function and return early from it.
def calc_sub_loop():
# your calculator here
elif sub_inp == 'Go Back':
go_to_sub_loop()
return
# Internal While loop
internal_loop = inputYesNo(f'\nDo you want to conduct {sub_inp} again ? [Yes/No]: ')
if internal_loop == 'yes':
print()
sub_loop()
else:
return
You can just write another sub loop function like this for your unit converter.
I also am going to put getting main_inp
in separate function as well.
def go_to_sub_loop():
# take main_inp
if main_inp == "calc":
calc_sub_loop()
elif main_inp == "unit":
unit_sub_loop()
The main while loop might look something like this.
choice_loop = 'yes'
while choice_loop == 'yes':
go_to_sub_loop()
choice_loop = inputYesNo('\nDo you want to access another Category ? [Yes/No]: ')
if choice_loop == 'yes':
continue
else:
break
This should work. This code is however untested so if you have any problems please let me know and I'll be happy to help you.
I hope this helps you!