i need help to understand where i am going wrong. i have been tasked with what i assume to be an easy task but still cannot figure it out, as my class is all doing it in C# and i was convinced to use python instead, and now i cannot look to my peers/teachers for support.
Anyways, i have been tasked with making a ‘work from home tracker’ for employees to log their hours, i have completed about 90% of the task, making a cmd terminal that allows user to log their hours, save the data to a .json file and re reading it and printing out a report for each employee for the week. what im stuck at is making a report to show all employees, and who worked the correct number of hours in the week.
see picture: task needed for assignment
not sure where to place this but ive seen others post their code:
import json
from os import path, read
### Varible Storage
employee_num = 1
filepath = "employeedata.txt"
greeting = """
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~ ~~
~~ Work Tracker ~~
~~ ~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
#Multi
#lined
#Comment
menu = """
[1] Add Work hours
[2] Read Records
[3] Exit Program
[4] Weekly Report
"""
print (greeting)
def read_json(fileNum, filename=filepath):
with open(filepath,"r") as file:
file_data = json.load(file)
if len(file_data) > 0:
return json.dumps(file_data["Employee Data"][0:fileNum], indent=3)
elif len(file_data) == 0:
return "No records found, please enter 1 in the menu to begin adding records."
else:
return "Something went wrong, please try again."
### Main Loop Begining
while True:
print (menu)
selection = input("Select which action you would like to perform [1/2/3/4] or Enter to return: ")
if selection == "1":
try:
welcome = "* * * Add Employee Working Hours * * * n n" #make hours worked before employee while loop starts
print (welcome)
while employee_num <=7:
### User input for tracking records
work_week = int(input("Enter current work week or press enter to return"))
print("n[Employee %d]" % employee_num)
employee_id = int(input("Enter Employee %d ID: " % employee_num))
employee_name = input("Enter Employee %d Name: " % employee_num)
Monday = int(input("Enter Hours Worked for Monday: "))
Tuesday = int(input("Enter Hours Worked for Tuesday: "))
Wednesday = int(input("Enter Hours Worked for Wednesday: "))
Thursday = int(input("Enter Hours Worked for Thursday: "))
Friday = int(input("Enter Hours Worked for Friday: "))
hours_worked = Monday + Tuesday + Wednesday + Thursday + Friday
### Storage of daily values as int in a dictionary for iteration
days = {
"Monday" : Monday, "Tuesday" : Tuesday, "Wednesday" :Wednesday, "Thursday" : Thursday, "Friday" : Friday
}
### Summary Text Storage
summaryP1 = "********************************n- Summary for Employee %d" % int(employee_id)
summaryP2 = ""
summaryP3 = Weekly_Report = "hours_worked" ###hours for whole week for report
summaryP4 = "- Total Hours Worked for Week %d: %d hours" % (int(work_week), int(hours_worked))+"nn"
print(summaryP1)
###summaryP5 = "Number of employees who worked under 30 hours"
###summaryP6 = "Number of employees who worked over 40 hours"
###summaryP7 = "Number of employees who worked between 30-40 hours"
## Checking Hours worked
for key, value in days.items():
if value > 10:
summaryP2 = "- Too Many Hours Worked on %s" % key
print (summaryP2)
elif value < 4:
summaryP2 = "- Insufficient Hours Worked on %s " % key
print (summaryP2)
elif value >4 and value <10:
summaryP2 = "- Suffient Hours Worked on %s" % key
print(summaryP2)
if hours_worked >40:
summaryP3 = "- Too Many Hours for This Week"
print(summaryP3)
elif hours_worked <30:
summaryP3 = "- Not Enough Hours for This Week"
print(summaryP3)
elif hours_worked >=30 <=40:
summaryP3 = "- Correct Amount of Hours This Week"
print(summaryP4)
###weekly report
### "Weekly Report" if hours_worked <30 in a week +1 to summaryP5 Number of employees who worked under 30 hours (summaryP5), elif hours_worked >40 in a week +1 to Number of employees who worked over 40 hours (summaryP6), elif >30 - >40 in a week +1 to Number of employees who worked between 30-40 hours (summaryP7)
### JSON dict construction data
initialData = {
"Employee Data":
[
{
"Working Week": work_week,
"Employee Number": employee_num,
"Employee ID": employee_id,
"Employee Name": employee_name,
"Monday": Monday,
"Tuesday": Tuesday,
"Wednesday": Wednesday,
"Thursday": Thursday,
"Friday": Friday,
"Hours Worked": hours_worked
}
]
}
### JSON dict data for additional entries into the dict
employeeData = {
"Working Week": work_week,
"Employee Number": employee_num,
"Employee ID": employee_id,
"Employee Name": employee_name,
"Monday": Monday,
"Tuesday": Tuesday,
"Wednesday": Wednesday,
"Thursday": Thursday,
"Friday": Friday,
"Hours Worked": hours_worked
}
### Initial JSON Dictionary Construction
def first_json(filename=filepath):
with open (filename, "w+") as file:
json.dump(initialData, file, indent = 3)
### Function for subsequent entries into dict
def write_json(new_data, filename=filepath):
with open (filename, "r+") as file:
file_data = json.load(file)
file_data["Employee Data"].append(new_data)
file.seek(0)
json.dump(file_data, file, indent = 3)
### checks if Json dict write is neccessary or if it should append subsequent entries
if employee_num == 1:
first_json()
elif employee_num >1:
write_json(employeeData)
employee_num +=1
###^while loops ends here
except Exception as error:
print("Fatal Error Occured. %s" % error)
elif selection == "2" and path.isfile(filepath):
number = int(input("Enter Number of Entries to Read: "))
print("n", read_json(number).replace('[',' ').replace(']',' ').replace('{',' ').replace('}',' '), "nn")
elif selection == "2" and not path.isfile(filepath):
print("File Not Found, Please Enter 1 in the Menu to Begin Adding Records.")
elif selection == "3":
print("closing program")
exit()
elif selection == "4":
print :hours_worked
###make weekly report here
###summaryP4
else:
print ("Number Entered was not Valid, Please Try Again." ) ```
i have tried watching countless tutorials, getting advice from teachers, got advice to try making edits from inside the main 'while loop', learning json data structures etc. im doing myself in knots but i really want to finish this course.
Jesse Whyte is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.