How to go back to the beginning of an if / elif statement?

https://www.codecademy.com/courses/learn-python/projects/calendar 19

Hi, guys.

I’m currently on the “Command Line Calendar” project, and I have a question about the code I showed in the picture (especially commands in line 40-42). Basically what shown in the picture is that the code prompts the user to input a “date” and “event”. If the “date” is of the wrong format, the code asks the user whether they want to try again.
And this is where I have a problem. If user input “Y”, the code will bring user to the beginning of the code and start over; if not, the while loop (not shown in the pic) is stopped. Is there a way to, when user inputs “Y”, instead of starting over from the very beginning, just start over from this elif statement?

Thanks!

You could put whatever you want to keep appearing inside a while loop. Create a variable and set it to True, while its true loop over the bit of code you want. When you’ve given it the input you want to move onto the next part of the code change the variable to False and it will end the while loop.

Lets say you’ve got a statement like

ta_key = True
while ta_key == True:
   if a > b:
      print 'invalid equation'
      try_again = raw_input('Would you like to try again?').upper()
      if try_again == 'N':
          ta_key = False

That should iterate over the portion of the code you want so long as they don’t type ‘N’ in when it asks if they’d like to try again.

1 Like

Got it. Thanks for the help!