0
tryAgain = True

right = True

while tryAgain:
print("I'm back")



 while right:
        again = input("\nDo you want to solve Spring Constant Formula again? [Y] / [N]: ")
        if again == "Y":
            print ("================================")
            continue
        elif again == "N":
            print ("================================")
            break
        
        else:
            continue #I want to get back to 'again' input

i am having a problem using the while loop inside while loop. i want to make the program going back to the 'again' input once the user did not choose between 'y' or 'n'

  • ```tryAgain``` value never changes. It is always True, the while loop goes forever –  Jul 31 '21 at 09:23
  • Please make sure the code here is exactly how it is in your IDE – StrangeSorcerer Jul 31 '21 at 09:23
  • Also, the loops aren't nested if that's what you were going for – StrangeSorcerer Jul 31 '21 at 09:24
  • As posted, you have two loops that execute sequentially (or they would, if the first loop didn't have an invariant exit test). If you want to nest them, then the inner loop needs to be *indented*, just like any other statement in the outer loop body. – Tom Karzes Jul 31 '21 at 09:27
  • My while's are indented. I don't know how to edit the code with right indention. – Maneki Autumn Jul 31 '21 at 09:29
  • Ideally you just copy/paste your code. That way it will be guaranteed to match what you're running. If you need to edit it by hand for some reason, then use the space key to indent. – Tom Karzes Jul 31 '21 at 09:31
  • It is edited now @TomKarzes – Maneki Autumn Jul 31 '21 at 09:31
  • It's a little unclear what you're trying to accomplish here. Do you really want nested loops? As it is, if the user enters anything other than `Y` or `N`, it will silently loop back to the input line, which seems to be what you want. It will do the same if the user enters `Y` (after printing the message). If the user enters `N`, it will exit the inner loop. Then it will continue to the next iteration of the outer loop, printing the "I'm back" message. – Tom Karzes Jul 31 '21 at 09:34
  • But to really control these loops, you need to change `tryAgain` and `right` where appropriate. If one of them never changes, then you don't need it and can just use `True` in its place. – Tom Karzes Jul 31 '21 at 09:35
  • I want the user to go back with the question if the user just inputted something. For example, the user inputted "a", the user will go back to the question input. But, if the user inputted right which is "y" it will print (i'm back) or "n" then the program will exit. @TomKarzes – Maneki Autumn Jul 31 '21 at 09:36
  • Well, the "bad input" case should already be working. To make the `Y` case go back to the outer loop, you need to do `right = False` followed by `break`, but then you also need to reset `right` to `True` inside the outer loop, before entering the inner loop. – Tom Karzes Jul 31 '21 at 09:39

0 Answers0