Python Python Sequences Sequence Iteration Iterating over Ranges

Stanley Nkosi
Stanley Nkosi
4,233 Points

Iterating over Range -Task 1 of 1 How do you append my_list ? Stanley Nkosi

The body of the for loop should append the current value to the provided list called my_list.append(val)

iterating_ranges.py
my_list = []
for i in range(0,10,1):
my_list.append(val):
  print(i)

4 Answers

Steven Parker
Steven Parker
205,345 Points

Stanley Nkosi — normally you'd choose "best answer" on the answer that helped solve the question! :see_no_evil:

Steven Parker
Steven Parker
205,345 Points

Here's a few hints:

  • each line of the body of the loop must be indented more than the "for" line
  • the "append" line should not end in a colon
  • where does the "val" come from that you are appending? It's not defined in this code
Stanley Nkosi
Stanley Nkosi
4,233 Points

Hi Steve, Thank you for the answer.I have indented the body.I have removed the semi-colon. Now,when I recheck it says,"Whoops it looks like my_list contains incorrect values or values are missing. The 'val' comes from the instruction that says,"To append a value to a list use syntax my_list.append(val).

Steven Parker
Steven Parker
205,345 Points

When I run it with the other fixes applied, I get: "Bummer: NameError: name 'val' is not defined".

The instruction example is intended to be generic, they are using the term "val" to represent the variable that will contain the value you want to append. You wouldn't use the actual term "val" unless you chose that as your loop variable name.

Stanley Nkosi
Stanley Nkosi
4,233 Points

Hi Steve, I have removed "val". When I recheck,I get the bummer that says,"Whoops looks like my_list contains incorrect values or is missing values.

Steven Parker
Steven Parker
205,345 Points

You "removed" it? Did you put something in it's place? (Hint: what is it you want to append?).

If you're still having trouble, repost the whole code in the current state.