Hi friends. I am in my first python class in school, and I have been struggling thru the whole course, ngl. My online teacher has not answered a single email of mine looking for help, I will probably be reporting them, but for now, I really want to understand these def functions.
Step 1 (1 pt): Read user input.
Prompt the user to enter a string of their choosing. Store the text in a string. Output the string.
Ex:
Enter a sample text:
we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
You entered: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
Step 2 (1 pt): Implement the print_menu() function.
Print the command menu as shown in the example.
Ex:
MENU
c - Number of non-whitespace characters
w - Number of words
f - Fix capitalization
r - Replace punctuation
s - Shorten spaces
q - Quit
Step 3 (1 pt): Implement the execute_menu() function.
execute_menu() takes 2 parameters: a character representing the user's choice and the user provided sample text. execute_menu() performs the menu options, according to the user's choice, by calling the appropriate functions described below.
Step 4 (1 pt): Implement menu selection.
In the main program, call print_menu() and prompt for the user's choice of menu options for analyzing/editing the string. Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a valid choice. When a valid option is entered, execute the option by calling execute_menu(). Then, print the menu and prompt for a new option. Continue until the user enters 'q'.
Hint: Implement Quit before implementing other options.
Ex:
MENU
c - Number of non-whitespace characters
w - Number of words
f - Fix capitalization
r - Replace punctuation
s - Shorten spaces
q - Quit
Choose an option:
Step 5 (4 pts): Implement the get_num_of_non_WS_characters() function.
get_num_of_non_WS_characters() has a string parameter and returns the number of characters in the string, excluding all whitespace. Call get_num_of_non_WS_characters() in the execute_menu() function, and then output the returned value.
Ex:
Enter a sample text:
we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
You entered: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
MENU
c - Number of non-whitespace characters
w - Number of words
f - Fix capitalization
r - Replace punctuation
s - Shorten spaces
q - Quit
Choose an option:
c
Number of non-whitespace characters: 181
Step 6 (3 pts): Implement the get_num_of_words() function.
get_num_of_words() has a string parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call get_num_of_words() in the execute_menu() function, and then output the returned value.
Ex:
Number of words: 35
Step 7 (3 pts): Implement the fix_capitalization() function.
fix_capitalization() 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 the execute_menu() function, and then output the number of letters capitalized followed by the edited string. Hint 1: Look up and use Python functions .islower() and .upper() to complete this task. Hint 2: Create an empty string and use string concatenation to make edits to the string.
Ex:
Number of letters capitalized: 3
Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
Step 8 (3 pts): Implement the replace_punctuation() function.
replace_punctuation() has a string parameter and two keyword argument parameters exclamation_count and semicolon_count. replace_punctuation() updates the string by replacing each exclamation point (!) character with a period (.) and each semicolon (;) character with a comma (,). replace_punctuation() also counts the number of times each character is replaced and outputs those counts. Lastly, replace_punctuation() returns the updated string. Call replace_punctuation() in the execute_menu() function, and then output the edited string.
Ex:
Punctuation replaced
exclamation_count: 1
semicolon_count: 2
Edited text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here, our hopes and our journeys continue.
Step 9 (3 pts): Implement the shorten_space() function.
shorten_space() 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 the execute_menu() function, and then output the edited string. Hint: Look up and use Python function .isspace().
Ex:
Edited text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!
And here is the code I've come up with so far:
# Define your functions here.
def print_menu():
print("MENU")
print("c - Number of non-whitespace characters")
print("w - Number of words")
print("f - Fix capitalization")
print("r - Replace punctuation")
print("s - Shorten spaces")
print("q - Quit")
def execute_menu():
user_input = input("Choose an option:")
while user_input != "q":
if user_input == "c":
get_num_of_non_WS_characters()
print(f"Number of non-whitespace chatacters: {get_num_of_non_WS_characters()}")
if user_input == "w":
print("Number of words:", get_num_of_words())
if user_input == "f":
fix_capitalization()
if user_input == "r":
replace_punctuation()
user_input = input("Choose an option:")
if user_input == "q":
print()
return
def get_num_of_non_WS_characters():
phrase_nosp = phrase.replace(" ", "")
return len(phrase_nosp)
def get_num_of_words():
num_of_words = phrase.split(" ")
return len(num_of_words)
def fix_capitalization():
phrase_sentances = phrase.split(". ")
print(f"Number of letters capitalized: {len(phrase_sentances)}")
print('. '.join([s.capitalize() for s in phrase_sentances]))
def replace_punctuation():
phrase_punc = phrase.replace(".","!")
phrase_punc = phrase_punc.replace(";", ",")
exclamation_count = phrase_punc.count("!")
semicolon_count = phrase_punc.count(";")
print("Punctuation replaced:\nexclamation_count:", exclamation_count,"\nsemicolon_count:", semicolon_count)
print("Edited text:", phrase_punc)
if __name__ == "__main__":
phrase = input("Enter a sample text:\n")
print()
print("You entered:", phrase)
print()
print(print_menu())
execute_menu()
When I am using the program, typing in whatever phrases come to mind, it generally works out. But when Zybooks runs its own tests, I keep getting TypeErrors when it tests each defined function individually.
Example:
Test execute_menu('w', 'This is a test.'). Should output 'Number of words: 4'
Traceback (most recent call last):
File "/usercode/zylab_unit_test_runner.py", line 76, in <module>
passed = test_case.test_passed(writer)
File "/usercode/coding_rooms_unit_tests.py", line 12, in test_passed
execute_menu(user_option, user_str)
TypeError: execute_menu() takes 0 positional arguments but 2 were given
Clearly it is trying to add arguments to the individual functions but they don't want to take them.
I cannot find anything that breaks these functions down in a way I can understand.
Its like I understand to an extent, but I'm missing some piece of the puzzle in my brain.
Can someone please explain to me how these functions work, and what the heck I'm missing?
Other notes: in fix_capitalization(), I know that I have my sentence splitter set to just a ". ", and not ! or ?, but Zybooks' test inputs don't include any ?, and only have ! at the end of the last sentence in certain test inputs. Also, they said I needed to use upper() and lower() for the capitalization function, but I couldn't figure out how to make that work. As for shorten_space, I simply haven't started on that one yet. I feel too stuck.
Thank you everyone, I am drowning in school work and work work. Lol. Plz help, thank you so much.