EDIT: It seems my error was caused by a simple oversight on my part & should be and in python. Thanks for the help everyone!
I'm trying to teach myself python and as such I want to make a simple script that calculates my hours worked for my current job. It seems I can't find any good sources on how to use regex expressions in conditional if and elif statements.
What I'm trying to do is check if my start time has a period of am or pm and whether my end time has a period of am or pm this way depending on the combination I can add the total hours worked up accurately.
To do this I am using regex to check the end of my input for am or pm. I then see which combination the times I sent in have, e.g. start_time = 8:00am and end_time = 11:23am so this should have matches for start_match_am and for end_match_am
My complete code is as follows:
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")
First I want to say I know I don't handle adding minutes together correctly at the moment, I wrote that when I was very tired and will be fixing when I get the regex to work. The issue specifically is happening at this line:
if start_match_am is not None & end_match_am is not None:
The error I get is:
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
This has really stumped me as the following posts on here specifically state this is how you use regex in conditional if statements:
Python: How to use RegEx in an if statement?
Regular Expression in conditional statement using Python 3
If anyone can point out a simple mistake I'm making or let others in a similar situation to me know how to properly use regex in conditional if statements in python 3 it would be much appreciated!