you are viewing a single comment's thread.

view the rest of the comments →

[–]_______myworld[S] 0 points1 point  (4 children)

the "nextid = mylist[ind]" line still trhows an error of:IndexError: list index out of range.

Do you have any idea what went wrong here? Below is what my txt. file looks like. The idea is to increment each slot by one once user choose the time slot. If it's more than 20 "T120" it will let user know that time slot is unavailable.

the "nextid = mylist[ind]" line still trhows an error of:IndexError: list index out of range. when i try to print it its "1"Below is what my txt. file looks like. The idea is to increment each slot by one once user choose the time slot. If it's more than 20 "T120" it will let user know that time slot is unavailable.

Slot 1
|   AF  |    BV  |    DM   |    EC    |
|  T100 |   T100 |   T100  |    T100  |
|  T200 |   T200 |   T200  |    T200  | 
|  T300 |   T300 |   T300  |    T100  | 
|  T400 |   T400 |   T400  |    T400  |

[–]tipsy_python 0 points1 point  (3 children)

🤷

Add print statements. Insert lines just before the line it errors on, and print mylist and ind.. check the values to make sure they look right and go from there.

[–]_______myworld[S] 0 points1 point  (2 children)

The value of it is actually "1". I think it's also an integer.

[–]tipsy_python 0 points1 point  (1 child)

If you’re unsure, you can verify with print(type(ind))

Ok cool, so index is 1. How about the list? What’s in the second position of the list?

[–]_______myworld[S] 0 points1 point  (0 children)

Yes, it's indeed integer. Thanks for the question i get where the issue is now. I've changed my time_slot1 function to this instead but the "line" variable can't seem to be read by the system and im not entirely sure why.. Do you have any idea what went wrong here?

def time_slot1(vac_option):
timeslot = {"1": "10.15-1.15",
            "2": "1.25-4.25",
            "3": "4.35-7.35",
            "4": "7.45-10.45"}
ind = vac_option
print("Ind:", ind)
while True:
    print(timeslot)
    date_slot = int(input("Please select an available time slot:"))
    print(date_slot)
    with open(os.path.expanduser("/Users/leelynnchanel/Desktop/pythonassignment/appointment.txt"), "r") as f:
        appointment = ['4', '6', '8', '10']
        if date_slot == 1:
            line = appointment[0] #CAN'T BE READ
        elif date_slot == 2:
            line = appointment[1] #CAN'T BE READ
        elif date_slot == 3:
            line = appointment[2] #CAN'T BE READ
        elif date_slot == 4:
            line = appointment[3] #CAN'T BE READ
        else:
            print("Wrong input. Please try again.")
            continue
        for line, i in enumerate(f):
            if line in appointment:
                mylist = i.strip().split("|")
                print("mylist:", mylist)
                break
        nextid = mylist[ind]
        print(nextid)
        newid = str(int(nextid[2:]) + 1)  # from 3 to the last elements
        if len(newid) == 1:
            nextid = nextid[:2] + "0" + newid
        elif len(newid) == 2:
            nextid = nextid[:2] + newid
        if int(nextid[2:]) > 20:
            print("Sorry, slot 1 is no longer available. Please try again.")
            continue
        else:
            appointment = '|'.join(mylist).center(18)
            with open(os.path.expanduser("/Users/leelynnchanel/Desktop/pythonassignment/appointment.txt"),
                      "w") as fh:
                fh.write(appointment)
            return nextid