PLEASE NOTE THAT THIS IS PYTHON 3 6.19 Program: Authoring assistant (Python 3) (
ID: 3576139 • Letter: P
Question
PLEASE NOTE THAT THIS IS PYTHON 3
6.19 Program: Authoring assistant (Python 3)
(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
def print_menu():
print('MENU c - Number of non-whitespace characters w - Number of words f - Fix capitalization r - Replace punctuation s - Shorten spaces q - Quit ')
menuOp = input('Choose an option: ')
while (menuOp != 'c' and menuOp != 'w' and menuOp != 'f' and menuOp != 'r' and menuOp != 's' and menuOp != 'q'):
menuOp = input('Choose an option: ')
return menuOp
def get_num_of_non_WS_characters(inputStr):
whitespace = 0
for character in inputStr:
if(character == ' ' or character == ' '):
whitespace += 1
return (len(inputStr) - whitespace)
def get_num_of_words(inputStr):
words = inputStr.split()
#print("words: ", words,end=' ')
return len(words)
def fix_capitalization(inputStr):
letCapped = 0
charArray = []
for character in inputStr:
charArray.append(character)
# Check first word, make sure its capitilized
if(charArray[0].islower()):
charArray[0] = inputStr[0].upper()
letCapped += 1
# Check over whole text
x = 0
capNext = 'false'
for character in charArray:
if(capNext == 'true' and character != ' ' and character != ' '):
charArray[x] = inputStr[x].upper()
letCapped += 1
capNext = 'false'
if(character == '.' or character == '!' or character == '?'):
capNext = 'true'
x += 1
# Reformat string
newString = ''
for character in charArray:
newString = newString + character
print('Number of letters capitalized:', letCapped, end = ' ')
print('Edited text:', newString, end=' ')
return newString, letCapped
def replace_punctuation(inputStr, exclamationCount, semicolonCount):
exclamationCount = 0
semicolonCount = 0
charArray = []
for character in inputStr:
charArray.append(character)
# Check over array for ! or ;
x = 0
for character in charArray:
if(character == '!'):
charArray[x] = '.'
exclamationCount += 1
elif(character == ';'):
charArray[x] = ','
semicolonCount += 1
x += 1
# Reformat string
newString = ''
for character in charArray:
newString = newString + character
# Print Output
print('Punctuation replaced', end = ' ')
print('exclamationCount:', exclamationCount, end=' ')
print('semicolonCount:', semicolonCount, end=' ')
print('Edited text:', newString, end=' ')
return newString
def shorten_space(inputStr):
words = inputStr.split()
newString = ''
for word in words:
if newString == '':
newString = word;
elif (newString != ''):
newString = newString + ' ' + word
print('Edited text:', newString, end=' ')
return newString
if __name__ == '__main__':
# (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)
inputStr = str(input('Enter a sample text: '))
print(' You entered:', inputStr, end=' ')
command = ''
while(command != 'q'):
command = print_menu()
if (command == 'c'):
# c = Number of non-whitespace characters
print('Number of non-whitespace characters:',get_num_of_non_WS_characters(inputStr),end=' ')
elif (command == 'w'):
# w = Number of words
print('Number of words:', get_num_of_words(inputStr), end = ' ')
elif (command == 'f'):
# f = Fix capitalization
fix_capitalization(inputStr)
elif (command == 'r'):
# r = Replace punctuation
replace_punctuation(inputStr, 0, 0)
elif (command == 's'):
# s = Shorten spaces
shorten_space(inputStr)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.