Date and Time wrong? in python

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật