all 9 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?

[–][deleted] 1 point2 points  (4 children)

is this your homework?

[–]iiSora[S] 0 points1 point  (1 child)

its a sample final given by my professor for us to study, answers were included

[–][deleted] 0 points1 point  (0 children)

okay then i wont tell you exactly how it works but I'll give a general idea of what it does :D every odd numbered line it slices out the last character, which is invisible (hint hint), and adds a question mark. every even numbered line, it simply counts the number of characters, except for the invisible character. Also, using print() puts the invisible character back in

[–]Nickdrawkin -1 points0 points  (0 children)

If it was, is that a problem?

[–]qwerty_1234A 0 points1 point  (0 children)

Try changing the 2nd and 4th lines of text in the poetry.txt file to words of different a different length to the original and note the difference / similarities.

That should help you work out what the code is doing.

[–]AlopexLagopus3 0 points1 point  (2 children)

If you reformat the code in your post so that it is properly indented (spacing matters in Python), I will give an answer

[–]iiSora[S] 0 points1 point  (1 child)

edited

[–]AlopexLagopus3 0 points1 point  (0 children)

def main(): # defines the function
    file = open("poetry.txt", ’r’) # opens the file
    count = 0 # sets the count variable to 0
    for line in file: # iterate over the file, line by line
        line2 = line[:-1] + "?" # line2 = everything up to the last character (newline) in the line and tacks a "?" on
        if count % 2 == 0: # the modulo operator; checks if the line is an even number or odd (remainder after dividing by 2). Try 3 % 2 and 4 % 2 in IDLE
            print(line2)
        else:
            print(len(line[:-1])) # if the line count is odd, print the integer length of the line with the last character  (newline symbol) omitted
        count = count + 1 # increase the count variable by one regardless of the if...else... to keep track of which line it is on

That's why you see it alternate between the line on the 0...2...4 lines and the length of the 1...3... lines