0

I was making a program that would calculate the money to be payed in a parking lot(not a real one). I have 2D arrays storing the data of the days of the week ([day, max hours, price per hour]), but when I try to check the day, I get a

TypeError: 'int' object is not subscriptable

The error was in line 15.

Here is the code for reference, although unfinished:

price_morning = [["Monday", 8, 2.00], ["Tuesday", 2, 10.00], ["Wednesday", 2, 10.00], ["Thursday", 2, 10.00], ["Friday", 2, 10.00], ["Saturday", 2, 10.00], ["Sunday:", 4, 3.00]]

price_midnight = [[8, 2.00], [8, 2.00], [8, 2.00], [8, 2.00], [8, 2.00], [8, 2.00], [8, 2.00]]

day = input("Day: ")

time = input("Time: ")

hours_of_stay = input("Hours of stay: ")

quest = input("Do you have a frequent parking number? ")

if quest == "Yes" :
    f_p_n = input("Frequent Parking Number: ")

else :
    f_p_n = 0

"Calculation: "

if int(time) < 16 :
    for i in range(len(price_morning)):
        if price_morning[i[0]] == day:
            if price_morning[i[1]] >= hours_of_stay:
                price = int(hours_of_stay) * int(price_morning[i[2]])

Is there anyway I could fix it?

Note:

f_p_n stands for frequent parking number, and will be used later on.

Zeyad
  • 1
  • 2
  • Welsome to SOF! the error would be indicating which `line-number` too . you could add that for more clarity – Sowjanya R Bhat Jul 01 '20 at 07:54
  • I don't understand which element of `price_morning` you expect to access using e.g. `price_morning[i[0]]`. This means to compute `i[0]` first, and then use that result to index into `price_morning`. – Karl Knechtel Jul 01 '20 at 07:57
  • Anyway, you should [not iterate that way, but instead directly grab the rows](https://stackoverflow.com/questions/4383250/why-should-i-use-foreach-instead-of-for-int-i-0-ilength-i-in-loops/4383321#4383321). In Python, you do that by [just using `in price_morning` for your `for` loop](https://nedbatchelder.com/text/iter.html). The resulting code is simpler, since you already have the row, and only need to index one more time. – Karl Knechtel Jul 01 '20 at 08:00

1 Answers1

0

price_morning[i[0]], price_morning[i[1]] and price_morning[i[2]] near the bottom are your problem.

The i[0] part in the square brackets will become 0[0] on the first run. ints aren't subscriptable, which explains your error.

You just need to move the brackets around a little:

price_morning[i][0]
price_morning[i][1]
price_morning[i][2]
James
  • 145
  • 5