Question

In: Computer Science

Hi there, please write code in Python 3 and show what input you used for the...

Hi there, please write code in Python 3 and show what input you used for the program. I've been stuck on this for hours!

(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!

Solutions

Expert Solution

SOLUTION-
I have solve the problem in python code with comments and screenshot for easy understanding :)

CODE-

#python code

import string
import re
OUT = 0
IN = 1

def print_menu(userStr):
menuOp=' '
print('\nMENU\nc - Number of non white-space Characters\nw - Number of words\nf - fix capatilization\nr - Replace punctuation\ns - Shorten spaces\nq - Quit')
while True:
menuOp=input('\nChoose an option: ')
if(menuOp in 'cwfrsq'):
break
print()
if (menuOp=='q'):
return menuOp,userStr
elif(menuOp=='c'):
print('Numebr of non-whitespace Characters:',get_num_of_non_WS_characters(userStr))
elif(menuOp=='w'):
print('Numebr of words:',get_num_of_words(userStr))
elif(menuOp=='f'):
newstr,count=fix_capilliation(userStr)
print('Numbee of latter capatalized:',count)
print('Edited text:',newstr)
elif(menuOp=='r'):
newstr,exCount,semiCount=replace_punctuation(userStr)
print('Punctuation repalced')
print('exclamationCount:',exCount)
print('semicolonCount:',semiCount)
print('\nEdited text:',newstr)
elif(menuOp=='s'):
newstr=shorten_space(userStr)
print('Edited text:',newstr)
return menuOp,userStr

def get_num_of_non_WS_characters(userStr):
userStrLen=len(userStr)
noOfSpace=userStr.count(' ')
return userStrLen-noOfSpace

def get_num_of_words(userStr):
state = OUT
wc = 0
# Scan all characters one by one
for i in range(len(userStr)):
# If next character is a separator, set the
# state as OUT
if (userStr[i] == ' ' or userStr[i] == '\n' or
userStr[i] == '\t'):
state = OUT
# If next character is not a word separator
# and state is OUT, then set the state as
# IN and increment word count
elif state == OUT:
state = IN
wc+=1
# Return the number of words
return wc
def fix_capilliation(user_input):
char_count=0
output_string = ''
line_terminator = ['.', '!']
end_of_line = False
for i in user_input:
if output_string == '':
char_count += 1
output_string += i.upper()
elif end_of_line == False and i not in line_terminator:
output_string = output_string + i
elif i in line_terminator:
end_of_line = True
output_string = output_string + i
elif end_of_line == True and i == ' ':
output_string = output_string + i
elif end_of_line == True and i != ' ':
output_string = output_string + i.upper()
char_count += 1
end_of_line = False
return output_string, char_count
def replace_punctuation(userStr,exclamationCount=0,semicolonCount=0):
newstr=' '
for ch in userStr:
if(ch=='!'):
newstr+='.'
exclamationCount+=1
elif(ch==';'):
newstr+=','
semicolonCount+=1
else:
newstr+=ch
return newstr,exclamationCount,semicolonCount
def shorten_space(userStr):
return re.sub(' +',' ',userStr)

def main():
print("Enter a simple text:")
userStr=input()
print('\nYou Entered:',userStr)

while True:
userChoie,userStr=print_menu(userStr)
if(userChoie=='q'):
break
if __name__=='__main__':
main()

SCREENSHOT-

CODE -

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

HOW WOULD YOU WRITE THIS IN PYTHON? SHOW THIS IN PYTHON CODE PLEASE # DEFINE 'all_keywords',...
HOW WOULD YOU WRITE THIS IN PYTHON? SHOW THIS IN PYTHON CODE PLEASE # DEFINE 'all_keywords', A LIST COMPRISED OF ALL OUR NEGATIVE SEARCH KEYWORDS AS REQUIRED BY THE PROJECT # FOR EACH ELEMENT IN 'full_list' (THE LIST OF LISTS YOU WOULD HAVE CREATD ABOVE THE PREVIOUS LINE) # INITIALIZE THE SET 'detected_keywords' TO EMPTY # DEFINE VARIABLE 'current_reviewer' FROM CURRENT ELEMENT OF 'full_list' BY EXTRACTING ITS FIRST SUB-ELEMENT,... # ...CONVERTING IT TO A STRING, AND THEN STRIPPING THE SUBSTRING...
Answer in Python: show all code 3) Modify your code to take as input from the...
Answer in Python: show all code 3) Modify your code to take as input from the user the starting balance, the starting and ending interest rates, and the number of years the money is in the account. In addition, change your code (from problem 2) so that the program only prints out the balance at the end of the last year the money is in the account. (That is, don’t print out the balance during the intervening years.) Sample Interaction:...
​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number....
​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number. The output of your code is the phone number’s  area code, prefix and line number separated with a space on a single line e.g INPUT 2022745512 OUTPUT 202 274 5512 write a code that will prompt a user to input 5 integers and output its largest value. PYTHON e.g INPUT 2 4 6 1 3 OUTPUT Largest value is: 6
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs....
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs. Create a GUI Calculator with the following: Title : Calculator Label and Entry box for 1st Number Label and Entry box for 2nd Number Buttons for *, /, +, - Label for result and Displaying the result
Write a Python code that will ask a user to input a year and return "Leap...
Write a Python code that will ask a user to input a year and return "Leap Year" if the year is a leap year, or "No Leap Year" if it is not a leap year. The program then asks for another year and performs the same task in the form of a loop, until the user inputs the then integer "-1." At that point, the program will exit the loop.
Please show the Rstudio code used in markdown. thank you! 3. Hypothesis Testing on Two Proportions...
Please show the Rstudio code used in markdown. thank you! 3. Hypothesis Testing on Two Proportions The Organization for Economic Cooperation and Development (OECD) summarizes data on labor-force participation rates in a publication called OECD in Figures. Independent simple random samples were taken of 300 U.S. women and 250 Canadian women. Of the U.S. women, 215 were found to be in the labor force; of the Canadian women, 186 were found to be in the labor force. a) Compute the...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please show all outputs. Instructions: Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18   List as Stack Code: """...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT