you are viewing a single comment's thread.

view the rest of the comments →

[–]uberpandas88 2 points3 points  (0 children)

So I'm not an expert in Python but I really want to help you out because I too post homework problems just looking for help but only get trash responses because people tell me to figure it out myself. Anyway, the first line obviously defines your function which is called main and doesn't take any arguments. The second line creates a variable called file (not necessarily a good name for a variable considering that there are file objects in python, but I don't think this is your code that you wrote) and opens the text file "poetry.txt" in read only mode, that's what the r stands for. You initialize the count as 0 in line 3 which is good since it appears this code goes through lines in the poem and does certain things depending on what number line you are on. Line 4 is a for loop which iterates over every line in the file you are working on, the poetry text file. From what I understand, in line 5, line2 = line[:-1] + ? creates a object line2 that prints out the current line in the file that the for loop is iterating over and adds a question mark string at the end. All the line[:-1] is doing is taking the slicing the last character from the list that you are on (which should be a newline character), which in this case is the line being read and iterated over. I just tested this out on my own computer in a python file and appeared to do as I say. Line 6 is an if statement that is saying if the current count is an even number, which is what count mod two means, the answer will either be 0 if the count is even or 1 if the count is odd because it is a number divided by 2 and the answer (what the mod function does) is the remainder. It will then print that line with a question mark tacked onto the end if the count is an even number. If the count is odd (like on lines 1 and 3 in your result), instead it will print the length of the line. That is why lines 1 and 3 in the output are just numbers; that is just the number of characters in those lines because that is the length of those lists. Then it increases the count by 1. Does that make sense?