В любом случае, мне было поручено создать «трекер работы на дому», чтобы сотрудники могли регистрировать свое время, я выполнил около 90% задачи, создав cmd-терминал, который позволяет пользователь может регистрировать свое рабочее время, сохранять данные в файл .json, перечитывать его и распечатывать отчет для каждого сотрудника за неделю. на чем я застрял, так это на создании отчета, в котором будут показаны все сотрудники и указано, кто отработал правильное количество часов в неделю.
см. рисунок: задача, необходимая для назначения
не знаю, где это разместить, но видел, как другие публиковали свой код:
Код: Выделить всё
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 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 40:
summaryP3 = "- Too Many Hours for This Week"
print(summaryP3)
elif hours_worked =30 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('}',' '), "\n\n")
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." )
Подробнее здесь: https://stackoverflow.com/questions/785 ... assignment