0

I'm making a program for school, and in one part I'm unable to get the input to loop back so that the user can edit their answer, and it just skips ahead to the next input after displaying the error message. I've tried every fix I can think of, but nothing seems to be working.

Here's the code and how it runs:

enter image description here

1

2 Answers 2

1

Use continue to go back to the start of the loop:

while True:
  InvDateStr = input("sfsdofjsadofj")

  if InvDateStr == "": # this error should be first
    print("can't be blank")
    continue

  try:
    InvDate = # baifaisjfoa
  except:
    print("that error")
    continue

  break

# rest of code
Sign up to request clarification or add additional context in comments.

1 Comment

I couldn't get a fix using this exact code format, but it did lead me to a fix (I'll add it to the answers if you're curious at all) so thank you!
0

Fix:

while True:
    InvDateStr = input("Enter the invoice date (YYYY-MM-DD): ")

    if InvDateStr == "":
        print("Invoice date cannot be blank. Please re-enter.")


    try:
        InvDate = datetime.datetime.strptime(InvDateStr, "%Y-%m-%d")
    except:
        print("Date must be in YYYY-MM-DD format. Please re-enter.")
        continue

    break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.