Lesson 3 Pl Help where is the error in my code


#1

Below is my code.

my_list = [i**2 for i in range(1,11)]

my_file = open("output.txt", "r+")

Add your code below!
for item in list:
my_file.write(str(item) + "\n")
my_file.close()

#2

Can you post both the link to the exercise and the error you are getting?


#3

The error in the console is "syntax error invalid syntax"


#4

Okay there are a couple of issues with your code.

Firstly the line "Add your code below!" needs to be either deleted or commented out.

Secondly, you've not indented your for loop on the second to last line.


#5

for item in list:
You need to indent the lines below
my_file.write(str(item) + "\n")
my_file.close()

also your writing the value of i and not the value of my_list at the i indices.
so its

my_file.write(str(my_file[item]) + "\n")


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.