Search This Blog
Exploring the Wonders of Science, Technology, and Human Potential
Featured
- Get link
- X
- Other Apps
Python Attendence RecoderPart1
1.
import json
Here, you are importing the `json` module, which allows you to work with JSON (JavaScript Object Notation) data in .
2.
def
load_attendance_data():
This line defines a function `load_attendance_data()` that will be responsible for loading attendance data from a JSON file.
3.
try:
with
open('attendance.json', 'r') as file:
data = json.load(file)
except
FileNotFoundError:
data = {}
- This block of code attempts to open the file `attendance.json` in read mode (`'r'`) using a context manager (`with` statement).
- If the file exists, it reads its contents using `json.load()` and
stores the data in the `data` variable.
- If the file is not found (`FileNotFoundError`), it initializes an
empty dictionary `data = {}`.
4.
return data
This line returns the loaded data (or an empty dictionary if the file doesn't exist) from the `load_attendance_data()` function.
5.
def
save_attendance_data(data):
Here, you define a function `save_attendance_data(data)` that will save attendance data to a JSON file.
6.
with
open('attendance.json', 'w') as file:
json.dump(data, file)
- This block of code opens the file `attendance.json` in write mode (`'w'`) using a context manager.
- It writes the contents of the `data` dictionary to the file in JSON format using `json.dump()`.
7.
def
update_attendance(subject):
This line defines a function `update_attendance(subject)` that updates attendance data for a given subject.
8.
data =
load_attendance_data()
Here, you load the attendance data from the JSON file using the `load_attendance_data()` function.
9.
if subject in data:
data[subject]
+= 1
else:
data[subject] = 1
- This block of code checks if the `subject` (presumably a class or
course name) exists in the attendance data.
- If it exists, it increments the attendance count for that subject.
- If it doesn't exist, it adds the subject to the data with an initial
count of 1.
10.
save_attendance_data(data)
After updating the attendance data, this
line calls the `save_attendance_data()` function to save the updated data back
to the JSON file.
11.
def main():
Here, you define the `main()` function, which serves as the entry point for your script
12.
while True:
password =
input("Enter password (or 'q' to quit): ")
if password
== 'q':
break
else:
update_attendance(password)
- This block of code creates an infinite
loop where the user is prompted to enter a "password" (which in this
context represents a subject or class name).
- Typing 'q' quits the loop, while any other input updates the attendance for that subject using the `update_attendance()` function.
13.
if __name__ == "__main__":
main()
This block of code ensures that the `main()` function is executed only when the script is run directly, not when it's imported as a module.
Each line of code plays a
specific role in loading, updating, and saving attendance data based on user
input.
- Get link
- X
- Other Apps
Popular Posts
What If India Loses this mindset of Reusing Things?
- Get link
- X
- Other Apps
Polar Bear is Suffering to Find Land Here is Why?
- Get link
- X
- Other Apps
Smarter move through technology revolution
- Get link
- X
- Other Apps
The Role of UX Design in Evolving Technology
- Get link
- X
- Other Apps
Comments
Post a Comment