Start New Discussion within our Software Development Community

Hello everyone i have an issue with this problem, the problem is i need to reset the count after every line in the file, i put a comment so you can see where i want to reset the count.

The program is suppose to cut each line after every specified lineLength.

def insert_newlines(string, afterEvery_char):
    lines = []
    for i in range(0, len(string), afterEvery_char):
        lines.append(string[i:i+afterEvery_char])
        string[:afterEvery_char] #i want to reset here to the beginning of every line to start count over
    print('\n'.join(lines))

def main():
    filename = input("Please enter the name of the file to be used: ")
    openFile = open(filename, 'r')
    file = openFile.read()
    lineLength = int(input("enter a number between 10 & 20: "))

    while (lineLength < 10) or (lineLength > 20) :
        print("Invalid input, please try again...")
        lineLength = int(input("enter a number between 10 & 20: "))

    print("\nYour file contains the following text: \n" + file + "\n\n") # Prints original File to screen
    print("Here is your output formated to a max of", lineLength, "characters per line: ")

    insert_newlines(file, lineLength)
main()

Ex. If a file has 3 lines like this with each line having 20 chars

andhsytghfydhtbcndhg 
andhsytghfydhtbcndhg
andhsytghfydhtbcndhg

after the lines are cut it should look like this

andhsytghfydhtb
cndhg
andhsytghfydhtb
cndhg
andhsytghfydhtb
cndhg

i want to RESET the count after every line in the file.

Rethink your logic, you can not loop every character of the file but every line, so iterate the file object to get those or pass list of lines. You can use recursion for line split or you can just slice the line, but you must save the slice current line until is shorter than current limit, your current line does not have any effect. I had similar excersise in MITx course, you are doing that?

This article has been dead for over six months. Start a new discussion instead.