-1

Hi I would like to request a time from the user in the format "HH:MM" using the input() function.

Do you have an idea with which function I can ensure that the user only enters numbers and only the special character ":" and not e.g. ";".

I create a funtion:

def checkElements(time):
    symbole = ":"
    res_list = []
    allowedKeys = ["0","1","2","3","4","5","6","7","8","9"]
    sizeAllowedKeys = len(allowedKeys)
    if symbole in time:
        res_list.append(symbole)
        for i in range(sizeAllowedKeys):
            if allowedKeys[i] in time:
                res_list.append(allowedKeys[i])
            elif allowedKeys[i] not in time:
                #print("false input")
                pass
        if len(res_list) == 5:
            keyStatus = 0
        elif len(res_list) != 5:
            print("false input")
            keyStatus = 1
    else:
        print("false input")
        keyStatus = 1
    return keyStatus

But here I have the problem that it does not recognize already recognized digits twice. Also I think that the solution is not very clean.

Thank you and best regards

PP90
  • 13
  • 4
  • Does this answer your question? [Limiting Python input strings to certain characters and lengths](https://stackoverflow.com/questions/8761778/limiting-python-input-strings-to-certain-characters-and-lengths) – UpmostScarab Feb 22 '23 at 22:15
  • "But here I have the problem that it does not recognize already recognized digits twice. " What does this mean? Can you give an example of something that should be valid input, but isn't, or vice-versa? "I would like to request a time from the user in the format "HH:MM"" in your own words, **why should** the input be in this format? **What will go wrong**, if the input is **not** in that format? **How will you use** the input? For example, if you will give it to some other code that raises an exception for bad input, did you consider *just handling that exception*? – Karl Knechtel Feb 22 '23 at 22:19
  • In your own words, where the code says `allowedKeys[i] in time`, what do you think this means? What is the purpose of this part of the code - what will it check? What is your plan for checking the input? (Hint: if you want to know *whether something is true about* all of the symbols in `time`, what makes more sense: should we write the loop to consider each of the symbols that is in `time`? Or should we write it to consider the contents of something else?) – Karl Knechtel Feb 22 '23 at 22:22

3 Answers3

0

As an option I'd suggest you use re module for that. This is not doing everything you want, but maybe it'll give you an idea how to achieve the same thing in a much easier way:

>>> import re
>>> regexp = re.compile("\d?\d:\d\d")
>>> right_str = "18:49"
>>> wrong_str = "AB;rp"
>>> regexp.match(right_str)
<re.Match object; span=(0, 5), match='18:49'>
>>> regexp.match(wrong_str)
UpmostScarab
  • 960
  • 10
  • 29
0

Try this:

def check(time):

    # time = 12:20
    # time.split(":") -> [12, 20]
    # "".join(time.split(":")) -> 1220
    if ":" in time and "".join(time.split(":")).isdigit(): 
        print("Great time")
    else:
        print("Wrong time")

check("12:20")
-1

This is a job for regular expressions

import re

string = input()

print(bool(re.match(r"^\d{2}:\d{2}$", string)))
# Prints True if the input was 2 digits, a colon, then 2 more digits, and nothing else
# Prints False otherwise
PedroTurik
  • 75
  • 5
  • Is 1:48 a valid time then? I mean you maybe want your user to input it as 01:48, which is fair, but still – UpmostScarab Feb 22 '23 at 22:24
  • it wont be, but just change it to "^\d?\d:\d{2}$" or "^\d?\d:\d\d$" and it will work. Take a look at https://regex101.com/, its super useful to get used to the patterns – PedroTurik Feb 22 '23 at 22:28
  • found a better option here https://stackoverflow.com/questions/7536755/regular-expression-for-matching-hhmm-time-format – PedroTurik Feb 22 '23 at 22:35