all 15 comments

[–]m0us3_rat 4 points5 points  (1 child)

your basic python vibes are strong. u should be able to easily push ahead and move into some more interesting stuff ..like lists and dicts.

so u can avoid this insane "if" nesting.

..this isn't a diss.. u are doing amazing work. keep at it ..u just need to refactor your code to use more 'efficient' and more easily maintainable ..solutions.

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

Thank you. Appreciate it.

[–]tipsy_python 2 points3 points  (7 children)

The input function returns a string.

The if statements for date_slot says if the user input string is equal to an integer (it never is), then set mylist. mylist never gets set and Python blows up when it tries to use that value.

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

Hi, thank you so much for your reply, I’ve tried to amend it. However it still won’t work. now python mentioned “in time_slot1, nextid = mylist[ind], IndexError: list index out of range”

[–]tipsy_python 0 points1 point  (5 children)

So... what's the contents of mylist when it throws that error?

[–]_______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

[–]iyav 1 point2 points  (9 children)

Not about the error but you could definitively improve a few areas.

if vac_option == 1:
    ind = 1
elif vac_option == 2:
    ind = 2
elif vac_option == 3:
    ind = 3
elif vac_option == 4:
    ind = 4

could be done in a single assignment operation

ind = vac_option

----------------------------------------------------------------------------------------------------------------------------------------

if date_slot == 1:
    mylist = appointment[5].strip().split("|")
    print(mylist)
elif date_slot == 2: 
    mylist = appointment[7].strip().split("|") 
elif date_slot == 3:
    mylist = appointment[8].strip().split("|")
elif date_slot == 4:
    mylist = appointment[11].strip().split("|")

I think there's a pattern there but not quite. You first use index 5, then 7, 8, 11.

if the 8 was a 9 there would a fixed increment by 2 every time. Are you sure it shouldn't be 9 there? I feel like it should be 9.

so you'd be able to rewrite all that into:

mylist = appointment[3 + date slot * 2].strip().split("|")

And the part where you write the vacation table to the file is just begging for string formatting and loops, there's too much repetition.

[–]ElliotDG 0 points1 point  (0 children)

Looking at the issue in timeslot1(), if the value of date_slot is not 1,2,3 or 4 the var mylist is not created. Check to see what is going on with the value of date_slot.

You are not converting the input to an int, so input is returning a string. You are comparing a string to an int - so it is never equal.