I was inputting the data from dictionary however the output of the date and time is just wrong. It showed the date which were all mondays and duplicates. Idk what is wrong with my code. Im a newbie.
<code>students = {
'Anna': {
'parent': 'Ms heather',
'tuition_fee': 40,
'lessons': [{'day': 'Thursday', 'start': '19:30', 'end': '21:00'}]
},
}
# Function to retrieve student information
def get_student_info(student_name):
# Retrieve the student's information from the dictionary
student_info = students.get(student_name)
# Check if the student exists in the dictionary
if student_info:
return student_info
else:
return "No information found for that student."
#Prompt user for the student name and payment month,year
student_name = input("Please enter the student's name: ")
year = input("Please enter the year: ")
month = input("Please enter the month: ")
from datetime import datetime, timedelta
def get_lesson_dates(lessons, month, year):
# Calculate the lesson dates and hours taught for a given month and year
lesson_dates = []
total_hours = 0
for lesson in lessons:
# Parse the lesson day to get the weekday number (0=Monday, 6=Sunday)
lesson_day_num = datetime.strptime(lesson['day'], '%A').weekday()
# Set up the starting date as the first day of the month
current_date = datetime(year, month, 1)
# Calculate end of the month
if month == 12:
end_date = datetime(year + 1, 1, 1) - timedelta(days=1)
else:
end_date = datetime(year, month + 1, 1) - timedelta(days=1)
# Loop over the days in the month
while current_date <= end_date:
if current_date.weekday() == lesson_day_num:
# Add the current date to the list of lesson dates
lesson_dates.append(current_date.strftime('%Y-%m-%d'))
# Calculate the duration in hours
start_time = datetime.strptime(lesson['start'], '%H:%M')
end_time = datetime.strptime(lesson['end'], '%H:%M')
duration = (end_time - start_time).total_seconds() / 3600
total_hours += duration
# Move to the next day
current_date += timedelta(days=1)
return lesson_dates, total_hours
# Convert user input to integers
try:
year = int(year)
month = int(month)
except ValueError:
print("Year and month should be numbers.")
exit()
# Retrieve the student's information from the dictionary
student_info = get_student_info(student_name)
# Check if the student exists in the dictionary and if the input is correct
if isinstance(student_info, dict):
# Extract the lessons and calculate the lesson dates and total hours
lesson_dates, total_hours = get_lesson_dates(student_info['lessons'], month, year)
# Output the result
print(f"Lesson dates for {student_name} in {month}/{year}: {lesson_dates}")
print(f"Total hours taught: {total_hours}")
else:
print(student_info)
</code>
<code>students = {
'Anna': {
'parent': 'Ms heather',
'tuition_fee': 40,
'lessons': [{'day': 'Thursday', 'start': '19:30', 'end': '21:00'}]
},
}
# Function to retrieve student information
def get_student_info(student_name):
# Retrieve the student's information from the dictionary
student_info = students.get(student_name)
# Check if the student exists in the dictionary
if student_info:
return student_info
else:
return "No information found for that student."
#Prompt user for the student name and payment month,year
student_name = input("Please enter the student's name: ")
year = input("Please enter the year: ")
month = input("Please enter the month: ")
from datetime import datetime, timedelta
def get_lesson_dates(lessons, month, year):
# Calculate the lesson dates and hours taught for a given month and year
lesson_dates = []
total_hours = 0
for lesson in lessons:
# Parse the lesson day to get the weekday number (0=Monday, 6=Sunday)
lesson_day_num = datetime.strptime(lesson['day'], '%A').weekday()
# Set up the starting date as the first day of the month
current_date = datetime(year, month, 1)
# Calculate end of the month
if month == 12:
end_date = datetime(year + 1, 1, 1) - timedelta(days=1)
else:
end_date = datetime(year, month + 1, 1) - timedelta(days=1)
# Loop over the days in the month
while current_date <= end_date:
if current_date.weekday() == lesson_day_num:
# Add the current date to the list of lesson dates
lesson_dates.append(current_date.strftime('%Y-%m-%d'))
# Calculate the duration in hours
start_time = datetime.strptime(lesson['start'], '%H:%M')
end_time = datetime.strptime(lesson['end'], '%H:%M')
duration = (end_time - start_time).total_seconds() / 3600
total_hours += duration
# Move to the next day
current_date += timedelta(days=1)
return lesson_dates, total_hours
# Convert user input to integers
try:
year = int(year)
month = int(month)
except ValueError:
print("Year and month should be numbers.")
exit()
# Retrieve the student's information from the dictionary
student_info = get_student_info(student_name)
# Check if the student exists in the dictionary and if the input is correct
if isinstance(student_info, dict):
# Extract the lessons and calculate the lesson dates and total hours
lesson_dates, total_hours = get_lesson_dates(student_info['lessons'], month, year)
# Output the result
print(f"Lesson dates for {student_name} in {month}/{year}: {lesson_dates}")
print(f"Total hours taught: {total_hours}")
else:
print(student_info)
</code>
students = {
'Anna': {
'parent': 'Ms heather',
'tuition_fee': 40,
'lessons': [{'day': 'Thursday', 'start': '19:30', 'end': '21:00'}]
},
}
# Function to retrieve student information
def get_student_info(student_name):
# Retrieve the student's information from the dictionary
student_info = students.get(student_name)
# Check if the student exists in the dictionary
if student_info:
return student_info
else:
return "No information found for that student."
#Prompt user for the student name and payment month,year
student_name = input("Please enter the student's name: ")
year = input("Please enter the year: ")
month = input("Please enter the month: ")
from datetime import datetime, timedelta
def get_lesson_dates(lessons, month, year):
# Calculate the lesson dates and hours taught for a given month and year
lesson_dates = []
total_hours = 0
for lesson in lessons:
# Parse the lesson day to get the weekday number (0=Monday, 6=Sunday)
lesson_day_num = datetime.strptime(lesson['day'], '%A').weekday()
# Set up the starting date as the first day of the month
current_date = datetime(year, month, 1)
# Calculate end of the month
if month == 12:
end_date = datetime(year + 1, 1, 1) - timedelta(days=1)
else:
end_date = datetime(year, month + 1, 1) - timedelta(days=1)
# Loop over the days in the month
while current_date <= end_date:
if current_date.weekday() == lesson_day_num:
# Add the current date to the list of lesson dates
lesson_dates.append(current_date.strftime('%Y-%m-%d'))
# Calculate the duration in hours
start_time = datetime.strptime(lesson['start'], '%H:%M')
end_time = datetime.strptime(lesson['end'], '%H:%M')
duration = (end_time - start_time).total_seconds() / 3600
total_hours += duration
# Move to the next day
current_date += timedelta(days=1)
return lesson_dates, total_hours
# Convert user input to integers
try:
year = int(year)
month = int(month)
except ValueError:
print("Year and month should be numbers.")
exit()
# Retrieve the student's information from the dictionary
student_info = get_student_info(student_name)
# Check if the student exists in the dictionary and if the input is correct
if isinstance(student_info, dict):
# Extract the lessons and calculate the lesson dates and total hours
lesson_dates, total_hours = get_lesson_dates(student_info['lessons'], month, year)
# Output the result
print(f"Lesson dates for {student_name} in {month}/{year}: {lesson_dates}")
print(f"Total hours taught: {total_hours}")
else:
print(student_info)
the output was all wrong for the dates. Do help me to solve this issue as idk what is wrong. Please comment below it will be greatly appreciated. I actually use chatgpt to generate this so I’m not so sure about the accuracy as well. Thanks for all the help guys 🙂
1