Question

In: Computer Science

*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the...

*******************In Python please*******************

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

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!

Solutions

Expert Solution

# function for counting the chars that are not whitspace
def get_num_of_non_WS_characters(text):
   word_list = text.split()
   count = 0
   for word in word_list:
      count += len(word)
   return count


# function for counting the number of words
def get_num_of_words(text):
   word_list = text.split()
   return len(word_list)


# function for fixing the capitalization of the words
def fix_capitalization(text):
   newText = ''
   fixed = 0
   sentences = text.split('.')
   for line in sentences:
      if line == '':
         continue
      if line[0] == ' ':
         new_line = line[0] + line[1].upper() + line[2:] + '.'
         fixed += 1
         newText += new_line
      else:
         new_line = line[0].upper() + line[1:] + '.'
         fixed += 1
         newText += new_line
   return newText, fixed


# function for removing he extra spaces
def shorten_space(text):
   words = text.split()
   newText = ''
   count = 0
   for letter in words:
      count += 1
      if count == len(words) -1:
         newText += letter  
      newText += letter + ' '
   return newText



# function for replacing the ! and ; symbols
def replace_punctuation(text):
   exclamation, semicolon = 0, 0
   newText = ''
   for char in text:
      if char == '!':
         newText += '.'
         exclamation += 1
      if char == ';':
         newText += ','
         semicolon += 1
      else:
         newText += char
   return newText, exclamation, semicolon


# menu function to display menu and read user response and further proceed
def print_menu(text):

   # loop to read till user quits
   while True:
      print()
      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')

      # read option
      options = ['c', 'w', 'r', 's', 'q', 'f']
      option = input('Choose an option: ')

      # validate option
      while option not in options:
         option = input('Choose an option: ')


      # if-else-if block to call the further functions according to the option entered by user
      if option == 'c':
         nwChars = get_num_of_non_WS_characters(text)
         print()
         print('Number of non-whitespace characters:', nwChars)

      elif option == 'w':
         numWords = get_num_of_words(text)
         print()
         print('Number of words:', numWords)

      elif option == 'f':
         fixedText, numFixed = fix_capitalization(text)
         print()
         print('Number of letters capitalized:', numFixed)
         print('Edited text:', fixedText)
      
      elif option == 'r':
         updatedText, exclamation_count, semicolon_count = replace_punctuation(text)
         print()
         print('Punctuations replaced')
         print('exclamation_count:', exclamation_count)
         print('semicolon_count:', semicolon_count)
         print('Edited text:', updatedText)
         
      elif option == 's':
         shorten_text = shorten_space(text)
         print()
         print('Edited text:', shorten_text)
      
      # if user wants to quit
      else:
         return


# driver function
def main():

   # read and display the text
   text = input('Enter a sample text:\n')
   print()
   print('Text:\n{}'.format(text))
   print()

   # call menu function
   print_menu(text)

FOR HELP PLEASE COMMENT.

THANK YOU/


Related Solutions

C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code 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...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of...
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...
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value...
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value from #the input() into a variable. #Refer to the variable in problem 2 to complete the following: #Use the if statement to evaluate the only the first element in the #grades list using index notation. #If the grade is passing, display PASSED, otherwise display FAILED. """Problem 1d""" #Use the for statement to iterate on each element in the #grades list (from problem 1a) and...
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value...
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value from #the input() into a variable. #Refer to the variable in problem 2 to complete the following: #Use the if statement to evaluate the only the first element in the #grades list using index notation. #If the grade is passing, display PASSED, otherwise display FAILED. """Problem 1d""" #Use the for statement to iterate on each element in the #grades list (from problem 1a) and...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Part I: Prompt the user for a single string and store it in a variable named...
Part I: Prompt the user for a single string and store it in a variable named userString. a. Use a for loop to print the string, char by char, with a dash '-' char between each. b. Use a for loop to print the string backwards, char by char, with a dash '-' char between each. Part II: Create an array of 5 strings named userStrings. Use a generalized array size. const int n = 5; string userStrings[n]; a. Populate...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the getNumOfCharacters() method, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in...
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 142.0 Enter weight 4: 166.3 Enter weight 5: 93.0 You entered: 236.0 89.5 142.0 166.3 93.0 (2) Also output the total weight, by summing the array's elements. (1 pt) (3) Also output the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT