I am attempting to create a loop that creates a file named "/tmp/newfile.txt" and creates 29 lines of text. Line 1 should read: "I see 0 sheep". For each line, 1 sheep should be added and a new line created to reflect that until 29 sheep (and lines) are reached.
x = 0
myDoc = myDoc.readfiles("/tmp/newfile.txt", "r ")
myDoc.write("I see" str(x) "sheep")
for line in myDoc.readfiles():
x = x 1
myDoc.append(x)
print(myDoc)
if x == 30
break;
First, what I tried to do is create a new file and put it into a variable (myDoc) that would open it. I specified w so that I would have the ability to read the file and write on it. I gave the changing number a variable 'x'.
The function I intended to, for each line in the file, write "I see x sheep". Afterward, add 1 to the current value of x and append it so it's added to the file. After this, print it so I can see the line(s). Once that value reached 30, cease the loop because 29 is the number of lines I need.
My errors have to do with indentation and nothing being printed at all. I am extremely new to this.
CodePudding user response:
Welcome to StackOverflow!
There seem to be a couple of issues in the code:
Indentation / Syntax Errors - It seems that you are using Python, which follows strict indentation and whitespace rules. An indent is inserted when you enter a new local scope / new control flow / enter an if/elif/else statement or a while or for loop, to separate it from the current scope. You'd need to remove the space on the left side on line 3 and line 6. Also, on line 8 there should be a colon(
:
) after theif x==30
.The mode used (
w
) isn't going to work as expected. This mode overwrites a file if it already exists and allows you to read and write to that file. Instead, you would need ther
mode. There's a great explanation & flowchart in this answer explaining the various file modes - https://stackoverflow.com/a/30566011/13307211The for loop can't iterate over
myDoc
. Theopen
function gives a file object (TextIOWrapper
), which can't be iterated over. You could use themyDoc.readfiles()
method, which returns a list of lines present in the file and loop over that -for line in myDoc.readfiles()
.printing myDoc and using
.append()
with myDoc wouldn't work as expected. It's representing a file object, which doesn't have an append method. Also, I feel like there might have been some mixed logic here - were you trying to iterate over myDoc like an array and hence pushing value to it? I'd suggest removing the append part as the past value ofx
isn't going to be needed for what you want to do.
After applying the above, you should end up with code that looks like this -
x = 0
myDoc = open("./newfile.txt", "r ")
for line in myDoc.readlines():
myDoc.write("I see" str(x) "sheep\n")
x = x 1
if x == 30:
break
Now, this doesn't exactly do what you want it to do...
The first thing we should do is update the for loop - a for loop should be structured in a way where it has a start, an end, and an increment, or it should iterate over a range of values. Python has a neat range function that allows you to iterate between values.
for x in range(1, 10):
print(x)
the above would print values from 1 to 10, excluding 10. updating our for loop, we can change the code to -
myDoc = open("./newfile.txt", "r ")
for x in range(1, 30):
myDoc.write("I see" str(x) "sheep")
we could also use a while loop here -
myDoc = open("./newfile.txt", "r ")
for x in range(1, 30):
myDoc.write("I see" str(x) "sheep")
this makes the file but without the lines and without the right formatting. "I see " str(x) " sheep"
should fix the sentence, but to print the string on multiple lines instead of the same line, you would need to use the newline character(\n
) and add it at the end of the string -
myDoc = open("./newfile.txt", "r ")
for x in range(1, 30):
myDoc.write("I see" str(x) "sheep\n")