Created a simple maths calc:
After user does their first calc they are asked 3 questions.
- Do more math with the final answer.
- Restart fresh
- Quit completely.
Used nest while loops to achieve this.
Picture below shows
- Success at doing a calc.
- User was then asked if they wanted to do more with the final answer. Answer was 'n'.
- They were then asked if they wanted to start fresh or quit completely. Answer was quit: 'n'.
- So program ended successfully.
However on retest
- Calc successful.
- Do want to continue with final answer = no.
- Do you want to start fresh? = yes.
- Program restarts. Great!
- Calc successful.
- Do want to continue with final answer = no.
- Do you want to start fresh? = no.
- At this point the program should end (as it did above) BUT it continued as if the user in step 2 above had said 'yes'.
Keeps looping and it shouldn't
Cannot work out why. Tried using breakpoints to see what is happening but still too inexperienced on how to correctly used this tool. Used Thonny as well. Thonny shows that it acknowledges the 'break' command (so it should slip out of the while loop) however it then skips ahead to the next coded line and the calc continues.
enter code here
more_calc = user_continue_check(data=math_question)
if more_calc == 3:
break
elif more_calc == 2:
main()
print("Error checking: When do we get to here?")
start_calc += 1
Is the problem in the nested while loops below? Just don't know. Been hacking away at this for hours. Using Thonny this nested loop works fine. It breaks out correctly returning the correct variable int result. So confused.
enter code here
def user_continue_check(data):
user_answer = False
ans = True
while not user_answer:
more_calc = str(input("\n\tDo you want to do more math with the calculated result of "
+ fg.orange + "{0}".format(data['result']) + fg.rs + "?" + fg.blue + " y/n: "
+ fg.rs)).strip().lower()
if more_calc == "n":
final_check = True
while final_check:
final_user_check = str(input("You you want to start a new calculation: y/n: ")).strip().lower()
if final_user_check == "n":
ans = 3
break
elif not final_user_check == "y":
print(fg.red + "\t\tYou need to enter y or n" + fg.rs)
else:
ans = 2
break
break
elif not more_calc == "y":
print(fg.red + "\t\tYou need to enter y or n" + fg.rs)
else:
ans = 1
break
return ans
Below is a link to the whole script. I just don't know where to look for the answer. Cannot work out why it does work then doesn't.