In Python (1) Prompt the user to enter a string of their choosing. Store the tex
ID: 3607722 • Letter: I
Question
In Python
(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)
Ex:
(2) Implement a print_menu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option and the sample text string (which can be edited inside the print_menu() function). Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement the Quit menu option before implementing other options. Call print_menu() in the main section of your code. Continue to call print_menu() until the user enters q to Quit. (3 pts)
Ex:
(3) 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 print_menu() function. (4 pts)
Ex:
(4) 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 print_menu() function. (3 pts)
Ex:
(5) Implement the fix_capilization() function. fix_capilization() has a string parameter and returns an updated string, where lowercase letters at the beginning of sentences are replaced with uppercase letters. fix_capilization() also returns the number of letters that have been capitalized. Call fix_capilization() in the print_menu() function, and then output the number of letters capitalized and 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. (3 pts)
Ex:
(6) Implement the replace_punctuation() function. replace_punctuation() has a string parameter and two keyword argument parameters exclamationCount and semicolonCount. 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_exclamation() in the print_menu() function, and then output the edited string. (3 pts)
Ex:
(7) 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 print_menu() function, and then output the edited string. Hint: Look up and use Python function .isspace(). (3 pt)
Ex:
Explanation / Answer
CODE
# definition of the function.
def get_num_of_non_WS_characters(str):
# count all non whitespace characters
count = len(re.findall('[^ ]',str))
# return count
return count
# definition of the function
def get_num_of_words(str):
# return number of words.
return len(str.split())
# definition of the function.
def fix_capilization(str):
# convert first letter of string.
str1=str[0].upper()
# Split the string.
l = str.split(".")
# empty string
tem = ""
c = 0
# start the for loop
for s in l:
# set the value of flag.
flag = True
# start the for loop.
for i in s:
# if ststement
if(i == ' '):
# assigning value to variable.
tem += i
# else if statement
elif(flag):
# check whether the character is in lower case or not.
if(i.islower()):
# Update the value of c.
c = c+1;
# convert the value in the upper case.
tem += i.upper()
# update the value of flag
flag = False
else:
# assign value to variable.
tem += i
tem += '.'
#Display the result
print ("Number of letters capitalized:",c)
# Display the concatenated string.
print ("Edited text:",str1+tem[1:-1])
return c;
# definition of the function
def replace_punctuation(str,exclamationCount=0,semicolonCount=0):
# Define an ampty string.
temp = ""
# start the for loop
for i in str:
# check exclamatory mark
if i == '!':
# update the value
exclamationCount += 1
temp += '.'
# Check for semi colon
elif i == ';':
# Update the value
semicolonCount += 1
temp += ','
else:
temp += i
# Display the result
print ("punctuation replaced.")
print ("exclamationCount:",exclamationCount)
print ("semicolonCount:",semicolonCount)
print ("Edited Text:",temp)
return temp
# Definition of the function
def shorten_space(str):
return ' '.join(str.split())
# Definition of the function.
def print_menu():
# Display the menu
menu = """------------MENU------------
c - Number of non-whitespace characters
w - Number of words
f - Fix capitalization
r - Replace punctuation
s - Shorten spaces
q - Quit
"""
print (menu)
# Prompt the user to enter the text.
str =input("Enter a sample text:")
# Display the entered text.
print ("You entered:",str)
# Display the menu.
print_menu()
# Start the while loop.
while(True):
# Prompt the user to enter an option.
c = input("Choose an option:")
if(c=='c'):
# Display number of non whitspace characters.
print("Number of non-whitespace characters:",get_num_of_non_WS_characters(str))
elif(c=='w'):
# Display number of words.
print ("Number of words:",get_num_of_words(str))
elif(c=='f'):
# fix capitalization of Text.
fix_capilization(str)
elif(c=='r'):
# Replace punctuatuion
replace_punctuation(str)
elif(c=='s'):
# removes extra space
print (shorten_space(str))
elif(c=='q'):
# Quit the program
print ("Good Bye!")
break
else:
# Display invalid input.
print ("Invalid Option,Try again")
IF ANY QUERIES REGARDING CODE AND EXECUTION PLEASE GET BACK TO ME
THANK YOU
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.