Thread: Program problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0

    Program problem


    fin=open('words.txt)
    for line in fin:
    word=line.strip()
    if len(word)>20
    print(word)


    I can run the above program in the idle shell/interpreter, but cannot seem to get it to work in a module. When I write fin=open('words.txt') at the start of a module the indentation goes strange and I cant write functions that refer to fin. How would I represent fin=open('words.txt') in a module and then refer to the contents of words.txt(which is just an alphabetical list of words.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    You've got two errors in your program, missing a quote and a colon. Are these problems in the original?

    On my GNU/linux system, having saved the program below as p.py and also having the unixdict file available, the program works fine. Do you have any words (lines) longer than 20 characters?

    $ python3 p.py
    electroencephalograph
    electroencephalography
    immunoelectrophoresis

    Code:
    with open('/home/lambertdw/Downloads/books/unixdict.txt') as fin:
        for line in fin:
            word=line.strip()
            if 20 < len(word):
                print(word)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0
    cheers, it worked, I swapped

    fin=open('words.txt'): for with open('words.txt') as fin:

    and the indentation started to work for the rest of the program. The program is basically taking and slicing parts of a list of crossword solutions, counterdemonstrations, hyperaggressivenesses and microminiaturisations are words apparently, though ive never seen them in a crossword. Thanks for the answer.

IMN logo majestic logo threadwatch logo seochat tools logo