jeudi 6 août 2020

I want to add my two list items in a dictionary

I am writing a code for a time table maker. This code takes the subjects that you want to study and the preference of studying(morning,afternoon,evening). Then I take total hours to be studied(20 hours) in a week. Then I divide it by 7 to calculate the study per day. Then I want to assign a random subject to my random time slot.

# Time Table Creator
import random
def TimeTableCreator(subjects,day,time_slots,total_hours):
    day = {}
    studyperday = total_hours/7
    studyperday = round(studyperday)

    subjects_study = random.sample(subjects,k=studyperday) # subjects that are selected randomly
    final_time_slots = random.sample(time_slots,k=studyperday) # list of my time slots
#trying to add both these items using for loop in a dictionary
    for subject in subjects_study:
        for i in range(studyperday):
         day[subject] = final_time_slots[i]

    print(day)
subjects = []
while True:
    subject = input("Enter a subject:\n")
    if subject=="":
        break
    subjects.append(subject)
    total_hours = 20


print("What is your preference of studying: Morning or afternoon or evening:")
time_preference = input()
if time_preference.lower()=="morning":
    time_slots = ["7:00-8:00","8:00-9:00","9:00-10:00","10:00-11:00","2:00-3:00"]
elif time_preference.lower()=="afternoon":
    time_slots = ["12:00-13:00","13:00-14:00","15:00-16:00","16:00-17:00","18:00-19:00"]
elif time_preference.lower()=="evening":
    time_slots = ["16:00-17:00","18:00-19:00","19:00-20:00","20:00-21:00","21:00-22:00"]
else:
    print("Invalid preference")
TimeTableCreator(subjects,"Monday",time_slots,total_hours)

Program output

Enter a subject:
phy
Enter a subject:
chem
Enter a subject:
bio
Enter a subject:

What is your preference of studying: Morning or afternoon or evening:
morning
**{'chem': '2:00-3:00', 'phy': '2:00-3:00', 'bio': '2:00-3:00'}**

Process finished with exit code 0

As you can see the time slot is the same that is assigned to different subjects. But I want that time slots should be different for each subject.I want that the time slots should be different for each subject. It is showing 2:00 to 3:00 for each subject. But I want different slots to be assigned for different subjects Please help.




Aucun commentaire:

Enregistrer un commentaire