1

I am writing a wage calculator and after working out how to validate the input format as HH:MM, I'm struggling to remember how to actually loop the code just once and then wait for another user input. Thanks for any help.

Code below;

while not bool(re.match("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", wed_start)):
    print("Please only use the HH:MM time format.")
wed_fin = input(ask_finish() + ' Wednesday?')
fmt = '%H:%M'
wed_hours = datetime.strptime(wed_fin, fmt) - datetime.strptime(wed_start, fmt)
print(tell_work(), wed_hours, 'hours on Wednesday.')
MarcoM
  • 101
  • 1
  • 7
  • I imagine your answer is in [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) but I don't recall it having a case for just one retry. If that is sufficient you can vote to close your question as a duplicate and we will too. – wwii Nov 05 '20 at 14:29
  • I'll be honest, I found that thread quite difficult to follow when iI initially found it. If you suggest that is where I'll find my answer then I'm sure you know better than me and I'll close this question as a duplicate. Thank you for your help. – MarcoM Nov 05 '20 at 14:31
  • ... You should try to adapt some of those scenarios to your requirements. I imagine the question/prompt in a function; the function called in a loop that is limited to two iterations. – wwii Nov 05 '20 at 14:34
  • I'll give that a go, cheers – MarcoM Nov 05 '20 at 14:41
  • If you figure something out that is unique enough, consider adding an answer to that question - maybe an answer that is a bit generic like limiting the questions to `n` responses with your example using `n = 2`. Then you and us can vote to close this one as a duplicate. – wwii Nov 05 '20 at 14:55
  • Will do, thanks a lot! – MarcoM Nov 08 '20 at 12:57

1 Answers1

1

Just a loop and break? Here is the sample of quite stupid but working implementation:

import re

attempts = 2

for a in range(attempts):
    wed_start  = input('give me HH:MM : ')
    validation = re.match("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", wed_start)
    if validation: break
    if a == 1:
        print('well, maybe next time')
        exit()
    else:
        print('try again, bud')

print('ok, here we go...')
# do stuff

Or a short variant of the same algorithm:

import re

for a in range(2):
    wed_start = input('HH:MM: ')
    if re.match("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$", wed_start): break
    if a == 1: exit()

# do stuff
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • This is really great thank you, this is what I was looking for. – MarcoM Nov 08 '20 at 12:57
  • Hey so I've added this to my code and it works great so thank you. Just one more question, as far as I understand it, it would be possible to define this is a function right? It seems like if I use this for every the start and finish of every day then my code will get bloated very quickly. – MarcoM Nov 09 '20 at 14:11
  • 1
    Of course. If you want to use it more than once it makes perfect sense to make a function. Let say it could return the validated string, or False (in case the user did wrong input). You need to figure out does it need some arguments? Number of attempts? Validation template? Input 'greeting'? Or none of it? – Yuri Khristich Nov 09 '20 at 16:03