简体   繁体   中英

Giving multiple inputs in a same line until the user is done in python

import re
time = input()
if re.match('^[1-2][0-9][:][0-9]{2}$',time):
    print('Late')
else:
    print('Ok')

I was working on a program in which there will be multiple timings (in 24 hour format. like 14:30,12:49 etc.) given by the user (the input range is from 4 to 9999) in a same line with a space between each of them. the problem is he doesn't give the number of inputs. He just types until he is done. so how to get the number of timings that are above 10:00. that says from 10:01.

the code i added only shows whether the time is above 10:00 or not. that too only for a single input. i am figuring out how to provide multiple inputs and run it where the all the inputs should be given in a single line

You can use list comprehension.

input().split will return a list of strings.

[time for time in input.split() if re.match('^[1-2][0-9][:][0-9]{2}$',time)] will return a list of strings from input.split() that match the RegEx.

len([time for time in input.split() if re.match('^[1-2][0-9][:][0-9]{2}$',time)]) will return the number of such strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM