编辑:我的错误似乎是由于我在python中的一个简单疏忽造成的。谢谢大家的帮助!在
我试图自学python,因此我想制作一个简单的脚本来计算我当前工作的工时。我似乎找不到任何关于如何在条件if和elif语句中使用正则表达式的好资料。在
我要做的是检查我的开始时间是否有一个上午或下午,我的结束时间是否有一个上午或下午这取决于组合我可以加上总工作时间准确。在
为此,我使用regex检查am或pm输入的结尾。然后我再看看我发来的是哪种组合,
e、 g.开始时间=上午8:00,结束时间=上午11:23,因此这应该有匹配的开始匹配项和结束匹配项
我的完整代码如下:import re
pattern = re.compile('^([0-1]?[0-9]):[0-5][0-9][aApPmM]{2}$')
start_time = ""
end_time = ""
hours_worked = 0.0
start_time = input("Please enter the time you started working(ex -- 8:00am): \n")
while re.match(pattern, start_time) == False:
print("I'm sorry the time must be in the format -- HH:MMam or H:MMpm \n")
start_time = input("Please enter the time you started working(ex -- 8:00am): \n")
end_time = input("Please enter the time you stopped working(ex -- 5:00pm): \n")
while re.match(pattern, end_time) == False:
print("I'm sorry the time must be in the format -- HH:MMam or H:MMpm \n")
end_time = input("Please enter the time you stopped working(ex -- 8:00am): \n")
start_hour = int(start_time[:(start_time.find(':'))])
start_minutes = int(start_time[(start_time.find(':') + 1):-2])
end_hour = int(end_time[:(end_time.find(':'))])
end_minutes = int(end_time[(end_time.find(':') + 1):-2])
start_period = start_time[start_time.find(':')+3:]
end_period = end_time[end_time.find(':')+3:]
pattern_am = re.compile('am', re.I)
pattern_pm = re.compile('pm', re.I)
start_match_am = re.match(pattern_am, start_period)
start_match_pm = re.match(pattern_pm, start_period)
end_match_am = re.match(pattern_am, end_period)
end_match_pm = re.match(pattern_pm, end_period)
if start_match_am is not None & end_match_am is not None:
total_hours = end_hour - start_hour
total_minutes = (end_minutes - start_minutes+60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
elif start_match_pm is not None & end_match_pm is not None:
total_hours = end_hour - start_hour
total_minutes = (end_minutes - start_minutes+60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
elif start_match_am is not None & end_match_pm is not None:
total_hours = end_hour + 12 - start_hour
total_minutes = (end_minutes - start_minutes + 60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
elif start_match_pm is not None & end_match_am is not None:
total_hours = end_hour + 12 - start_hour
total_minutes = (end_minutes - start_minutes + 60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
首先我想说的是,我知道我目前无法正确地将分钟相加,当我非常累的时候,我会在使用regex时进行修复。问题具体发生在这条线上:
^{pr2}$
我得到的错误是:Please enter the time you started working(ex -- 8:00am):
8:00am
Please enter the time you stopped working(ex -- 5:00pm):
11:23am
Traceback (most recent call last):
File "/home/primus/PycharmProjects/HoursWorks/venv/HoursWorked/CalculateHours.py", line 27, in
if start_match_am is not None & end_match_am is not None:
TypeError: unsupported operand type(s) for &: 'NoneType' and '_sre.SRE_Match'
Process finished with exit code 1
这真的让我很为难,因为下面的帖子在这里特别说明了在条件if语句中如何使用regex:
如果有人能指出我犯的一个简单的错误,或者让其他类似情况的人知道如何在python3中的条件If语句中正确使用regex,我们将不胜感激!在