0

Is there is a way to make sure that the user enters the input data as I want them to, For example, I wrote this code so the user can enter some birthdays and the script will choose one in random:

import random, re

print("keep in mind that you need to enter the date in this format dd/mm/yyyy")

cont_1 = input("please enter the informations of the 1st contestant : \n")
cont_2 = input("please enter the informations of the 2nd contestant : \n")
cont_3 = input("please enter the informations of the 3rd contestant : \n")
cont_4 = input("please enter the informations of the 4th contestant : \n")
cont_5 = input("please enter the informations of the 5th contestant : \n")

print("Thank you,")

win = cont_1 + " " + cont_2 + " " + cont_3 + " " + cont_4 + " " + cont_5

contDates = re.compile(r'\d\d/\d\d/\d\d\d\d')
ir = contDates.findall(win)
print(" And the Winner is: ", random.choice(ir))

I want to know if there is a way to force the user to write in the input in this format ../../... when he enters the first two digits a slash shows and the next two

AMC
  • 2,642
  • 7
  • 13
  • 35
  • You could use a `while` loop to keep asking for the input as long as it doesn't have the correct format. However, it might also make sense for your program to be able to handle / identify different input formats. – mapf May 02 '20 at 21:49
  • @martineau This is not a duplicate, although the answers are the same, the question is something else. – MegaIng May 02 '20 at 22:07
  • 1
    @MegaIng The duplicate target is not that different. The only different part is a validation rule. I strongly believe that the closure is justified. – Georgy May 02 '20 at 22:18
  • @MegaIng: I disagree — the gist, or central idea (as well as the solution) is identical. – martineau May 02 '20 at 22:22
  • As an aside, unless there is a good reason not to do so, variable and function names should follow the `lower_case_with_underscores` style. – AMC May 03 '20 at 00:36

3 Answers3

1

You can check if it is correct date format like this without using regex.

import datetime
user_input = input()

try:
  datetime.datetime.strptime(user_input,"%d/%m/%Y")
except ValueError as err:
  print('Wrong date format')
  • https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – AMC May 03 '20 at 00:37
1

There is not easy way to do this. The easiest solution is to just check that what the user input is correct before asking for the next input:

date_re = re.compile(r'\d\d/\d\d/\d\d\d\d')
def ask_date(prompt):
    while True: # Ask forever till the user inputs something correct.
        text = input(prompt)
        if date_re.fullmatch(text): # Does the input match the regex completly (e.g. no trailing input)?
             return text # Just return the text. This will break out of the loop
        else:
             print("Invalid date format. please use dd/mm/yyyy")

cont_1 = ask_date("please enter the informations of the 1st contestant : \n")
cont_2 = ask_date("please enter the informations of the 2nd contestant : \n")
cont_3 = ask_date("please enter the informations of the 3rd contestant : \n")
cont_4 = ask_date("please enter the informations of the 4th contestant : \n")
cont_5 = ask_date("please enter the informations of the 5th contestant : \n")

This also simplifies the selection process, since all dates are valid:

print(" And the Winner is: ", random.choice((cont_1, cont_2, cont_3, cont_4, cont_5))
MegaIng
  • 7,361
  • 1
  • 22
  • 35
  • https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – AMC May 03 '20 at 00:37
-1

If you want it custom:

i = input("date (dd/mm/yyyy):")
split = i.split("/")
for item in split:
    try:
        int(item)
    except:
        print("error")
        exit()
if len(split) != 3 or len(split[0]) not in [1, 2] or len(split[1]) not in [1, 2] or len(split[2]) != 4:
    print("error!")
else:
    print("accepted!")

This makes sure that all of the items are numbers, and that there are 3 slashes, the first and second ones are two digits, and the last one is 4 digits. If you want to accept any correct date:

xilpex
  • 3,097
  • 2
  • 14
  • 45
  • What makes this better than a regex? It's harder to read and it doesn't verify that it is just numbers. – MegaIng May 02 '20 at 21:56
  • @MegaIng -- It's faster to implement, and for non-regex readers, it is easy to read. – xilpex May 02 '20 at 21:57
  • You are going to have a hard time to convince me that anyone will look at this piece of code and immediately understand what it does. For a simple regex, this should be the case. Also, the regex version is easy to maintain and change in the future. (and your version allows negative numbers in dates right now). – MegaIng May 02 '20 at 22:06
  • https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – AMC May 03 '20 at 00:37