0
Help fix my code.
I want to accept date strings in either of %m/%d/%Y or %B/%d/%Y formats and print in %d/%m/%Y. Currently I can only accept in %m/%d/%Y format. from datetime import datetime a = input() date = datetime.strptime(a, '%m/%d/%Y').strftime('%d/%m/%Y') print(date)
1 Réponse
+ 1
This is a code coach challenge, so I can give you not a ready to use code. But I will give you some hints how you can solve it - you are not far away.
You want to accept 2 (or could be also more) different input date formats, that you are going to check with strptime(). So you should put the both format schemes in a list. Then you do a for loop, taking the first scheme from the list and try to read it with strptime().
As you can fail with an input that does not match the strptime() scheme, you have to put the check with strptime in a try: except: block.
try: will get the strptime() and strftime() and in the next line the print statement.
The except: block only gets a "continue", so we do not react if an exception is raised. We silently go back to the to the for loop read the next scheme and do the same as with the first scheme.
It sounds difficult, but it's just adding the for loop, the try: and the except: block.
If you struggle with this (but I believe you get it) come back finish it.