In: Computer Science
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!!
(1) Prompt the user to enter a string of their choosing. Store
the text in a string. Output the string. (1 pt)
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!
(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:
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:
(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:
Number of non-whitespace characters: 181
(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:
Number of words: 35
(5) 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 print_menu() function, and then output
the the edited string followed by the number of letters
capitalized. 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:
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!
(6) 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 print_menu() function, and then output
the edited string. (3 pts)
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.
(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:
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!
def getNumOfNonWSCharacters():# function to number of non white
space characters
c=0; #counter
a=input()#getting text
d=list(a); # define list of character of input string
#print(d)
for i in d: #calculated no of character except white space
if i != ' ': # check if current character is white space
c=c+1
print("Number of non-whitespace characters: "+str(c)); # print the
character count
def getNumOfWords(): # function to calculate the words in the
string
a=input()#getting text
b=(a.split())#split the string on the basis of space between
them
print("Number of words: "+str(len(b)));#print the output of no of
words in the string
def findText():# function to perform the occurence of word in the
text
a=input()#getting text
b=input()#getting text
c=a.count(b) #calculate the occurence
print(" "+b+" instances: "+str(c))#print the no of occurence of
words in the text
def replaceExclamation(): #function to remove all the ! with the
.
a=input()#getting text
print(a.replace("!",".")) #perform the replacement of ! with
.
def shortenSpace():
a=input()#getting text
print(' '.join(a.split())) #perform the remove of all the wide
white soace from the text
a1=input("Enter the text:") # getting the text
print("You entered: "+str(a1)) # orinting the result on the
screen
print("MENU\nc - Number of non-whitespace characters\nw - Number of
words\nf - Find text\nr - Replace all !'s\ns - Shorten spaces\nq -
Quit ")
b=input() #menu input
if(b=='c'):#compare if it is for character count
getNumOfNonWSCharacters()# calling the function
elif b=='w':#compare if it is for word count
getNumOfWords()# calling the function
elif b=='f': #compare if it is for find the text
findText()# calling the function
elif b=='r': #compare if it is for replacement of ! with .
replaceExclamation() # calling the function
elif b=='s': #compare if it is for removal of wide white space with
single space
shortenSpace() # calling the function
elif b=='q': #compare if it is for quit
exit()
else:
print("Enter optoin is invalid")
If you found this answer helpful please give a thumbs up.