#๐Ÿ”’ error correction

38 messages ยท Page 1 of 1 (latest)

agile bolt
#

doing an assignment in trying to find errors

def main():
task_list: list[str] = []
task_list.append("Complete Task 5.3PP")
task_list.append("Watch 'The Holy Grail'")
task_list.append("Write more Python")

print(f"You have {len(task_list)} tasks on your list:")
for i in range(len(task_list)):
print(f"{i+1}. {task_list[i+1]}")

is the reason this not running because its trying to take 1 extra then is actually in the list

hexed belfryBOT
#

@agile bolt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

agile bolt
#

just want to make sure but

#

for example there is 3 things in the list here

#

but its trying to take from the 4th place

#

technically 2 things cause 0, 1, 2 but

#

is this correct?

rocky bronze
#

i could be wrong but i think you have to have a starting index and ending index when using range()

#

ah nm guess not

agile bolt
#

not sure honestly in my head its like

if you have 3 boxes the code is trying to take an item from the 4th box but there isn't a 4th box so its throwing an error my way

dire ferry
#

range(len(task_list)) will count starting with 1

#

While index numbers starts at 0

agile bolt
#

is that something specific to "range"

#

like it always starts at 1

dire ferry
#

No it's len()

agile bolt
#

oh

#

its throwing an error on the line afterwards though so maybe they're both wrong

dire ferry
#

I think I explained it a bit badly, but you are trying to get one more item than you have available

agile bolt
#

nah that makes sense

#

thank you

dire ferry
#

It is the +1 part

agile bolt
#

so if i get rid of that it fixes it

neat shore
#

yes

dire ferry
#

But are you familiar with enumerate()?

agile bolt
#

its meant to be wrong so we can fix it

dire ferry
#
for idx, item in enumerate(task_list, start=1):
    print(f"{idx}. {item}")
dire ferry
agile bolt
#

fair point

#

so this lets you start specifically at a certain part in the list?

stoic hornet
dire ferry
#

Each iteration will give you back index number in the list, and the object.
The start=1 argument adds 1 to each index number

stoic hornet
#

The problem is in task_list[i+1]

dire ferry
#

We came to that in the end too, good you point it out

dire ferry
agile bolt
#

!close

hexed belfryBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.