PYTHON i understand this is a long question so i get it if you can't answer all of it, but if you could at least help me get started i'd really appreciate it (sample.txt is the name of the file we have to download) (1) Prompt the user to enter the name of a file. This will be the file we use as input to our program. Determine if the file name inputted is valid. If it is, print a statement confirming this to the user. If the file is not valid, print an error to the user and ask for a new input (This should repeat for repeated invalid file names) Ex: Enter the name of a file: sample.txt Opening file: sample.txt Enter the name of a file: garbage.notafile The file "garbage.notafile" is not valid! Enter the name of a file: ... Hint: functions in the os.path module may help determine if the file is valid (2) The file can now be read by our program, implement a function read_file(file_name) that takes in the string containing the file name as a parameter and returns a string with the contents of the file. This function will read the file removing any newline characters in the file. Ex: sample.txt Look again at that dot. That's here. That's home. That's us. Given the following code: text_string = read_file(file_name) print(text_string) This will output: Look again at that dot. That's here. That's home. That's us. (3) Implement a print_menu() function, This function will print out the menu of options exactly as below and returns the user's entered menu option. Each option is represented by a single character. If an invalid character is entered, continue to prompt for a valid choice. Call print_menu() in the main section of your code. Continue to call print_menu() until the user enters q to Quit. Ex: MENU c - Number of non-whitespace characters w - Number of words f - Fix capitalization s - Shorten spaces o - Output edited text q - Quit Choose an option: Hint: Implement the Quit menu option before implementing other options.
PYTHON
i understand this is a long question so i get it if you can't answer all of it, but if you could at least help me get started i'd really appreciate it (sample.txt is the name of the file we have to download)
(1) Prompt the user to enter the name of a file. This will be the file we use as input to our program. Determine if the file name inputted is valid. If it is, print a statement confirming this to the user. If the file is not valid, print an error to the user and ask for a new input (This should repeat for repeated invalid file names)
Ex:
Enter the name of a file: sample.txt Opening file: sample.txt Enter the name of a file: garbage.notafile The file "garbage.notafile" is not valid! Enter the name of a file: ...
Hint: functions in the os.path module may help determine if the file is valid
(2) The file can now be read by our program, implement a function read_file(file_name) that takes in the string containing the file name as a parameter and returns a string with the contents of the file. This function will read the file removing any newline characters in the file.
Ex: sample.txt
Look again at that dot. That's here. That's home. That's us.
Given the following code:
text_string = read_file(file_name) print(text_string)
This will output:
Look again at that dot. That's here. That's home. That's us.
(3) Implement a print_menu() function, This function will print out the menu of options exactly as below and returns the user's entered menu option. Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a valid choice. Call print_menu() in the main section of your code. Continue to call print_menu() until the user enters q to Quit.
Ex:
MENU c - Number of non-whitespace characters w - Number of words f - Fix capitalization s - Shorten spaces o - Output edited text q - Quit Choose an option:
Hint: Implement the Quit menu option before implementing other options.
(4) Implement the count_non_WS_characters(text_string) function which has a string parameter and returns the number of characters in the string, excluding all whitespace. Call count_non_WS_characters() in you main loop of your program and print the returned value.
Ex:
Number of non-whitespace characters: 595
(5) Implement the count_words(text_string) function. count_words(text_string) has a string parameter and returns the number of words in the string. Call count_words() in the main loop of your program.
Ex:
Number of words: 114
Hint: Words end when a space is reached except for the last word in a sentence.
(6) Implement the fix_capitalization(text_string) function. fix_capitalization(text_string) has a string parameter and returns an updated string, where lowercase letters at the beginning of sentences are replaced with uppercase letters. fix_capitalization() also returns the number of letters that have been capitalized. Call fix_capitalization() in your main loop of your program, and then output the the edited string followed by the number of letters capitalized.
Ex: sample.txt
look again at that dot. that's here. That's home. that's us. Number of letters capitalized: 3 Edited text: Look again at that dot. That's here. That's home. That's us.
Hint: Look up and use Python functions .islower() and .capitalize() to complete this task.
(7) Implement the shorten_space(text_string) function. shorten_space(text_string) has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. shorten_space() returns the string. Call shorten_space() in your main loop of your program, and then output the edited string.
Ex: sample.txt
Look again at that dot. that's here. that's home. that's us. Edited text: Look again at that dot. that's here. that's home. that's us.
Hint: Look up and use Python function .isspace().
(8) Implement the output_text(text_string, output_file) function. output_text() has a string parameter for the text from the file and another string parameter for the file name. This function will write the contents of the text_string variable in your program with any edits made to the output file specified. Call output_text() in your main loop of your program, then print to the user that the output was successful. If an error occurs, your program should print an error and return to the menu. Hint: Use exception handling.
Ex:
Choose an option: o Enter the output file name: output.txt Output successful to: output.txt
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images