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

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Question About Ignoring Newline Characters in Word Game


    Hello,

    So I made another function regarding my Word Game.

    I want to return a board aka a horizontal continous list of words I have in a file named 'boardex' that are separated by different lines to begin with.

    I have both my code, as well as the text file I am referring to ('boardex') included below:


    Here is my code so far:

    Code:
    def read_board(board_file):
        """ (file open for reading) -> list of list of str
    
        Return a board read from open file board_file. The board file will contain
        one row of the board per line. Newlines are not included in the board.
        """
        board_file = open(board_file, 'r')
    
    
        line = board_file.readline()
        # This will assign a line to the current read
        while line != '':
    
            # This is a while loop that will continue as long as there are no
            # blank characters
    
            line = line.rstrip('\n')
            line = print(line, end = '')
            return line
            line = board_file.readline()

    What's inside 'boardex.txt':
    BOT
    COL
    DOL
    DRL
    DRU
    DRA
    DRM
    KOL
    LON
    NOL
    MON
    MAN
    NOM
    SOL
    WON
    TON
    RUN
    GUN
    FUN
    HUN
    PUN
    SUN
    TOP
    POT
    NOT
    ROT
    TAR
    MAN
    LOT
    TOY
    YUP
    PAT
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Anyone know?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    Honestly speaking, your post does not contain a question, so it is hard to tell what you are asking about

    You can read your file in a list like the following:

    Code:
    words = list(open(board_file))
    Then you can iterate it in any way you like. Stripping etc.

    Code:
    words = [w.rstrip("\n") for w in list(open(board_file))]

IMN logo majestic logo threadwatch logo seochat tools logo