35.9k views
0 votes
Program:

Authoring assistant (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string.
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 PrintMenu() 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. Each option is represented by a single character. If an invalid character is entered, continue to prompt for a valid choice.
Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to call PrintMenu() until the user enters q to Quit.
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.
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.
Ex:
Number of words: 35

(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.
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_exclamation() in the print_menu() function, and then output the edited string.
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().
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!

User Ryan Cook
by
5.6k points

2 Answers

5 votes

Python program that uses the above functionalities is shown below

def get_user_input():

user_input = input("Enter a sample text: ")

return user_input

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")

return input("\\Choose an option: ")

def get_num_of_non_WS_characters(text):

return len([char for char in text if not char.isspace()])

def get_num_of_words(text):

words = text.split()

return len(words)

def fix_capitalization(text):

count = 0

edited_text = ""

sentences = text.split(". ")

for sentence in sentences:

if sentence:

sentence = sentence[0].upper() + sentence[1:]

count += 1

edited_text += sentence + ". "

return count, edited_text.strip()

def replace_punctuation(text, exclamation_count=0, semicolon_count=0):

text = text.replace('!', '.')

exclamation_count = text.count('.')

text = text.replace(';', ',')

semicolon_count = text.count(',')

print("\\Punctuation replaced")

print(f"exclamation_count: {exclamation_count}")

print(f"semicolon_count: {semicolon_count}")

return text

def shorten_space(text):

return ' '.join(text.split())

def main():

user_text = get_user_input()

while True:

option = print_menu()

if option == 'c':

print(f"Number of non-whitespace characters: {get_num_of_non_WS_characters(user_text)}")

elif option == 'w':

print(f"Number of words: {get_num_of_words(user_text)}")

elif option == 'f':

count, edited_text = fix_capitalization(user_text)

print(f"Number of letters capitalized: {count}")

print(f"Edited text: {edited_text}")

user_text = edited_text

elif option == 'r':

user_text = replace_punctuation(user_text)

print(f"Edited text: {user_text}")

elif option == 's':

user_text = shorten_space(user_text)

print(f"Edited text: {user_text}")

elif option == 'q':

print("Goodbye!")

break

else:

print("Invalid option. Please try again.")

if __name__ == "__main__":

main()

User Jashandeep Sohi
by
6.8k points
5 votes

Answer:

See explaination

Step-by-step explanation:

#method to print menu & handle user choice

def print_menu(input_str):

#printing 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\\')

#getting choice

choice=input('Choose an option: ').lower()

#identifying choice

if choice=='c':

#displaying number of non white space chars in input_str

print('Number of non-whitespace characters:',get_num_of_non_WS_characters(input_str))

elif choice=='w':

#displaying number of words in input_str

print('Number of words:',get_num_of_words(input_str))

elif choice=='f':

#fixing capitalization and getting updated string and count of values capitalized

input_str,count=fix_capilization(input_str)

#displaying results

print('Number of letters capitalized:',count)

print('Edited text:',input_str)

elif choice=='r':

#replacing punctuation, displaying updated text

input_str=replace_punctuation(input_str)

print('Edited text:', input_str)

elif choice=='s':

#shortening spaces and displaying updated text

input_str = shorten_space(input_str)

print('Edited text:', input_str)

#returning choice and input_str

return choice,input_str

#returns the number of non white space chars in input_str

def get_num_of_non_WS_characters(input_str):

count=0

#looping through each character in input_str

for i in input_str:

if not i.isspace():

#i is not a space

count+=1

return count

#returns the number of words in input_str

def get_num_of_words(input_str):

#splitting words into list of tokens by space

words=input_str.split(' ')

count=0

#counting all non empty strings in words list

for i in words:

if len(i)>0:

count+=1

return count

#method to fix capitalization and return updated string and count of letters updated

def fix_capilization(input_str):

count=0

beginning=True #starting letter should be capitalized

result=''

for i in input_str:

if beginning and i.isalpha():

#start of a sentence and i is alphabetic

if i.islower():

#converting i to upper case, incrementing count

i=i.upper()

count+=1

result+=i

#not start of a sentence

beginning=False

elif i in '?.!':

#i is either ? or . or !, next letter should be capitalized

beginning=True

result+=i

else:

#any other character

result+=i

return result,count

#method to replace exclamation and semicolons with period and comma respectively

def replace_punctuation(input_str,exclamationCount =0,semicolonCount=0):

result=''

for i in input_str:

if i=='!':

i='.'

exclamationCount+=1

elif i==';':

i=','

semicolonCount+=1

result+=i

print('Punctuation replaced')

#displaying replaced values counts

print('exclamationCount:',exclamationCount)

print('semicolonCount:',semicolonCount)

return result

#removes all double or more spaces in input_str

def shorten_space(input_str):

input_str=input_str.strip()

result=''

prev=None

for i in input_str:

if prev==None:

result+=i

elif i==' ':

if prev != ' ':

result+=i

else:

result+=i

prev=i

return result

if __name__ == '__main__':

#getting input, printing it

input_str=input('Enter a sample text:\\')

print('\\You entered:',input_str)

choice=' '

#looping until choice becomes q

while choice!='q':

choice,input_str=print_menu(input_str)

User Amr Mostafa
by
6.3k points