【发布时间】:2018-09-30 21:58:26
【问题描述】:
编辑:看来我的错误是由我的一个简单的疏忽引起的,应该是在 python 中。感谢大家的帮助!
我正在尝试自学 Python,因此我想制作一个简单的脚本来计算我在当前工作中的工作时间。似乎我找不到任何关于如何在条件 if 和 elif 语句中使用正则表达式的好资料。
我要做的是检查我的开始时间是否有上午或下午,以及我的结束时间是否有上午或下午,这取决于我可以准确添加总小时数的组合.
为此,我使用正则表达式来检查我输入的结束时间是上午还是下午。然后我看看我发送的时间有哪个组合, 例如start_time = 8:00am 和 end_time = 11:23am 所以这应该匹配 start_match_am 和 end_match_am
我的完整代码如下:
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")
首先我想说我知道我现在不能正确地把分钟加在一起,我在我很累的时候写了这个,当我让正则表达式工作时会修复。这个问题具体发生在这一行:
if start_match_am is not None & end_match_am is not None:
我得到的错误是:
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 <module>
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 语句中使用正则表达式的方式:
Python: How to use RegEx in an if statement?
Regular Expression in conditional statement using Python 3
如果有人能指出我正在犯的一个简单错误,或者让与我处于类似情况的其他人知道如何在 python 3 中的条件 if 语句中正确使用正则表达式,我们将不胜感激!
【问题讨论】:
-
&应该是and -
tl;dr - 但是关于测试字符串是否以
am或pm结尾的条件,不要使用re。使用str.endswith('am')或str.endswith('pm')。例如。In: '8:00 pm'.endswith('pm') --> Out: True和In: '8:00 pm'.endswith('am') --> Out: False。当然,字符串变量而不是常量也是如此。 -
@JETM 哇,现在我觉得自己很蠢,不敢相信我没有查到。
-
还有@SpghttCd str.endswith() 是否适用于不匹配的情况?
标签: python regex python-3.x