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 getinputtext:
# Prompt the user for input text
text inputEnter a sample text:
print
You entered:", text
return text
# Function to print the menu with options
def printmenu:
print
MENU"
printc Number of nonwhitespace characters"
printw Number of words"
printf Fix capitalization"
printr Replace punctuation"
prints Shorten spaces"
printq Quit
# Function to execute a menu option based on user's choice
def executemenuoption textinput:
# Option for counting nonwhitespace characters
if option c:
result getnumofnonWScharacterstextinput
printNumber of nonwhitespace characters:", result
# Option for counting number of words
elif option w:
result getnumofwordstextinput
printNumber of words:", result
# Option to fix capitalization capitalizing first letter of each sentence
elif option f:
updatedtext, capcount fixcapitalizationtextinput
printNumber of letters capitalized:", capcount
printEdited text:", updatedtext
# Option to replace punctuation exclamation marks with periods and semicolons with commas
elif option r:
updatedtext, exclamationcount, semicoloncount replacepunctuationtextinput
printPunctuation replaced"
printexclamationcount:", exclamationcount
printsemicoloncount:", semicoloncount
printEdited text:", updatedtext
# Option to shorten spaces reduce multiple spaces to a single space
elif option s:
updatedtext shortenspacetextinput
printEdited text:", updatedtext
# Invalid option handler
elif option q:
return None
else:
printInvalid option. Please try again."
return textinput # Return the updated text if modified
# Function to count the number of nonwhitespace characters
def getnumofnonWScharacterstext:
return lenchar for char in text if not char.isspace
# Function to count the number of words in a string
def getnumofwordstext:
words text.split # Split the string by whitespace
return lenwords
# Function to fix capitalization by capitalizing the first letter of each sentence
def fixcapitalizationtext:
updatedtext
capcount
firstlet True # Flag to track whether we are at the start of a new sentence
for i char in enumeratetext:
# Capitalize the first letter after a sentenceending punctuation or
if firstlet and char.islower:
char char.upper
capcount
firstlet False # We've capitalized, now we are not at the start of a sentence
# If we encounter a sentenceending punctuation mark, the next character should be capitalized
if char in :
firstlet True # The next character is the start of a new sentence
updatedtext char
return updatedtext, capcount
def replacepunctuationtext exclamationcount semicoloncount:
updatedtext
# Loop through each character in the text
for char in text:
if char : # If it's an exclamation mark
updatedtext # Replace with a period
exclamationcount # Increment exclamation mark count
elif char ;: # If it's a semicolon
updatedtext # Replace with a comma
semicoloncount # Increment semicolon count
else:
updatedtext char # If it's any other character, keep it unchanged
return updatedtext, exclamationcount, semicoloncount # Return the updated text and counts
# Function to shorten spaces by reducing multiple consecutive spaces to a single space
def shortenspacetext:
words text.split # This splits the text by any whitespace and removes extra spaces
return joinwords
# Main function to control the program flow
def main:
textinput getinputtext # Get the input text from the user
while True:
printmenu # Display the menu
choice inputChoose 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
textinput executemenuchoice textinput
# Call the main function to start the program
if namemain:
main
i have an issue with printmenu and an error message saying "Tests that replacepunctuation replaces exclamation point and 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
