May-17-2019, 05:43 PM
(This post was last modified: May-17-2019, 10:28 PM by Drone4four.)
Since my last forum post, I have taken my script to the next level. Rather than just analyzing a basic 6 line text file, I present the user with choices of three very long text files (full length books). I managed to dynamically adjust the range depending on the length of each book. I’ve also added doc strings to each of my functions.
I am really pleased with how my script is cooking so far. See the bottom of this post for my script in full.
Although there are still some issues I am trying to handle.
Near the end of the script, the user is prompted to replay using the
Another issue with the script is at line 57 where I attempt to re-declare the
I’m working with three text files (public domain books). The three books can be found here on my Dropbox. Tolstoy’s book is some 66054 lines long. I chose this because because it is so large in order to put my Python interpreter to work. It’s 3.4 MBs so it’s too big to attach here on this Python forum message board.
Here is my latest iteration of my script:
I am really pleased with how my script is cooking so far. See the bottom of this post for my script in full.
Although there are still some issues I am trying to handle.
Near the end of the script, the user is prompted to replay using the
again()
function. When the user enters “C” (or anything other than A, B, or C), then the script should exit. That’s what I’m expecting. But instead (like when I enter “aaa” or whatever as input), the validate_and_choose()
function is triggered. Could someone lend a hand with an explanation?Another issue with the script is at line 57 where I attempt to re-declare the
chosen_book
variable. There my linter says that chosen_book
is an unused variable (even though it was already declared and used earlier in the script). I figure this could be because the variable is declared within again()
and is therefore out of scope, meaning it is isolated within that particular function and has no access to variables declared or used elsewhere in the script. Is this correct? If this variable is out of scope as I suspect, then how do I get this working properly so that the book()
function is triggered (as I have set out to do)?I’m working with three text files (public domain books). The three books can be found here on my Dropbox. Tolstoy’s book is some 66054 lines long. I chose this because because it is so large in order to put my Python interpreter to work. It’s 3.4 MBs so it’s too big to attach here on this Python forum message board.
Here is my latest iteration of my script:
# This is my ninth iteration of this text reading script. def book(): ''' This function presents the user with a selection of 3 potential books to examine ''' options = {'1':'Tolstoy.txt','2':'Alice.txt','3':'Chesterton.txt'} print("Choose from this list of books: \n 1. Tolstoy \n 2. Alice \n 3. Chesterton") pick = input("What is your pick? 1.? 2.? or 3.?") selection = options.get(pick) print(f"You picked: {selection[:-4]}!") # for testing return selection def showcase(): ''' This function essentially prints the entire book, line by line (but also prints the associated line numbers ''' with open(chosen_book, 'r') as lines: lines2 = lines.readlines() for num, line in enumerate(lines2): print(num, line.rstrip()) return lines2 def how_long(lines2): ''' This function counts the number of lines (for future use) ''' length = len(lines2) return length def validate_and_choose(request, allowed_range): ''' This function ensures the user input is an integer and within the range of number of lines ''' range_text = f'integer in range {min(allowed_range)} - {max(allowed_range)}' while True: answer = input(f'{request} (enter {range_text}) ') try: answer = int(answer) if answer in allowed_range: return answer if answer not in allowed_range: raise ValueError except ValueError: print(f'Expected {range_text} but input was "{answer}". Try again! ') def main(choice): ''' This function opens, reads each line and then processes the user's choice and presents the final user's output ''' with open(chosen_book, 'r') as lines: picked_line = lines.readlines()[choice] picked_line = picked_line.rstrip() result_num = picked_line.split() length_of_line = len(result_num) num_chars = len("".join(picked_line)) print(f'Here is the line that you picked:\n "{picked_line}" \nThe number of words: {length_of_line}\nThe number of characters: {num_chars}') def again(): ''' This function gives the user the ability to (1) start from the very beginning at the top, (2) restart half way through or (3) exit ''' replay = str(input("\nWould you like to choose a new line in the same book? Or would you prefer to pick a line from a different book? Enter:\n'A' for the same book, \n'B' for a different book, or \n'C' to exit this program \n Make your selection: ")) if replay == "A" or "a": final = validate_and_choose('\nWhich line do you want to count and print? ', range(0,length)) main(final) again() elif replay == "B" or "b": chosen_book = book() again() pass elif replay == "C" or "c": print("Goodbye!") exit else: print("I'll take that answer as a request to exit this program. Goodbye for now!") pass if __name__ == "__main__": chosen_book = book() num_lines = showcase() length = how_long(num_lines) final = validate_and_choose('Which line do you want to count and print? ', range(0,length)) main(final) again()