(Python 3) Hi, I need help with the following question. The following code is al
ID: 3571742 • Letter: #
Question
(Python 3) Hi, I need help with the following question.
The following code is already given:
#Type all other functions here
def print_menu(usrStr):
menuOp = ' '
#Complete print_menu() function
return menuOp, usrStr
if __name__ == '__main__':
#Complete main section of code
Explanation / Answer
Here are the few functions for you:
#!/usr/bin/python
def get_num_of_non_WS_characters(usrStr):
count = 0
for i in range(usrStr):
if usrStr[i] != ' ':
count += 1
return count
def get_num_of_words(usrStr):
count = 0
for i in range(usrStr):
if usrStr[i] == ' ':
count += 1
return count + 1
def fix_capitalization(usrStr):
count = 0
if usrStr[0].islower():
usrStr[0] = usrStr[0].upper()
count += 1
for i in range(usrStr):
if usrStr[i] == '.':
if usrStr[i+2].islower():
usrStr[i+2] = usrStr[i+2].upper()
count += 1
return usrStr, count
def print_menu(usrStr):
menuOp = ' '
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'
menuOp = input('Choose an option: ')
if menuOp == 'c':
print 'Number of non-whitespace characters: ', get_num_of_non_WS_characters(usrStr)
elif menuOp == 'w':
print 'Number of words: ', get_num_of_words(usrStr)
elif menuOp == 'f':
usrStr, count = fix_capitalization(usrStr)
print 'Number of letters capitalized: ', count
elif menuOp == 'r':
print 'To be implemented'
elif menuOp == 's':
print 'To be implemented'
elif menuOp == 'q':
return menuOp, usrStr
else:
print_menu(usrStr)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.