Question: my code: # Function to prompt for text input and return it def get _ input _ text ( ) : # Prompt the user

my code: # Function to prompt for text input and return it
def get_input_text():
# Prompt the user for input text
text = input("Enter a sample text:
")
print("
You entered:", text)
return text
# Function to print the menu with options
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
")
# Function to execute a menu option based on user's choice
def execute_menu(option, text_input):
# Option for counting non-whitespace characters
if option =='c':
result = get_num_of_non_WS_characters(text_input)
print("Number of non-whitespace characters:", result)
# Option for counting number of words
elif option =='w':
result = get_num_of_words(text_input)
print("Number of words:", result)
# Option to fix capitalization (capitalizing first letter of each sentence)
elif option =='f':
updated_text, cap_count = fix_capitalization(text_input)
print("Number of letters capitalized:", cap_count)
print("Edited text:", updated_text)
# Option to replace punctuation (exclamation marks with periods and semicolons with commas)
elif option =='r':
updated_text, exclamation_count, semicolon_count = replace_punctuation(text_input)
print("Punctuation replaced")
print("exclamation_count:", exclamation_count)
print("semicolon_count:", semicolon_count)
print("Edited text:", updated_text)
# Option to shorten spaces (reduce multiple spaces to a single space)
elif option =='s':
updated_text = shorten_space(text_input)
print("Edited text:", updated_text)
# Invalid option handler
elif option =='q':
return None
else:
print("Invalid option. Please try again.")
return text_input # Return the updated text if modified
# Function to count the number of non-whitespace characters
def get_num_of_non_WS_characters(text):
return len([char for char in text if not char.isspace()])
# Function to count the number of words in a string
def get_num_of_words(text):
words = text.split() # Split the string by whitespace
return len(words)
# Function to fix capitalization by capitalizing the first letter of each sentence
def fix_capitalization(text):
updated_text =""
cap_count =0
first_let = True # Flag to track whether we are at the start of a new sentence
for i, char in enumerate(text):
# Capitalize the first letter after a sentence-ending punctuation ('.','!', or '?')
if first_let and char.islower():
char = char.upper()
cap_count +=1
first_let = False # We've capitalized, now we are not at the start of a sentence
# If we encounter a sentence-ending punctuation mark, the next character should be capitalized
if char in '.!?':
first_let = True # The next character is the start of a new sentence
updated_text += char
return updated_text, cap_count
def replace_punctuation(text, exclamation_count=0, semicolon_count=0):
updated_text =""
# Loop through each character in the text
for char in text:
if char =='!': # If it's an exclamation mark
updated_text +='.' # Replace with a period
exclamation_count +=1 # Increment exclamation mark count
elif char ==';': # If it's a semicolon
updated_text +=',' # Replace with a comma
semicolon_count +=1 # Increment semicolon count
else:
updated_text += char # If it's any other character, keep it unchanged
return updated_text, exclamation_count, semicolon_count # Return the updated text and counts
# Function to shorten spaces by reducing multiple consecutive spaces to a single space
def shorten_space(text):
words = text.split() # This splits the text by any whitespace and removes extra spaces
return "".join(words)
# Main function to control the program flow
def main():
text_input = get_input_text() # Get the input text from the user
while True:
print_menu() # Display the menu
choice = input("Choose an option:
") # Prompt for a menu option
if choice =='q': # If the user chooses 'q', break out of the loop and quit
break
# Execute the selected menu option
text_input = execute_menu(choice, text_input)
# Call the main function to start the program
if __name__=='__main__':
main()
i have an issue with print_menu() and an error message saying "Tests that replace_punctuation() replaces 1 exclamation point and 2 semicolons in "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 by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!