2) Complete the get_num_of_characters() function, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts) (3) Extend the program by calling the get_num_of_characters() function and then output the returned result. (1 pt) (4) Extend the program further by implementing the output_without_whitespace() function. output_without_whitespace() outputs the string's characters except for whitespace (spaces, tabs). Note: A tab is '\t'. Call the output_without_whitespace() function in main(). (2 pts)

Respuesta :

Explanation:

Number of characters: 46

String with no whitespace: Theonlythingwehavetofearisfearitself.

########################################################

def get_num_of_characters(inputStr):

   count = 0

   for i in range(0,len(inputStr)):

       count = count + 1

   #count = len(inputStr)

   return count

def output_without_whitespace(inputStr):

   #statement = statement.strip()

   statement2 = ' '

   for i in range(0,len(inputStr)):

        if(inputStr[i] == ' '):

           statement2 = statement2

       else:

           statement2 = statement2 + inputStr[i]

       

   inputStr = inputStr.replace(" ", "")

   return statement2

inputStr = input('Enter a sentence or phrase: ')

print()

print('You entered:', inputStr)

num = get_num_of_characters(inputStr)

print()

print('Number of characters:', num)

print('String with no whitespace:',output_without_whitespace(inputStr))

#if __name__ == '__main__':

   # Think I'm supposed to use this if statement

########################################################

'''ERROR MESSAGE WHEN RUNNING TEST

Unit test

0/1

Tests that get_num_of_characters() returns 46 for "The only thing we have to fear is fear itself."

Your output

Enter a sentence or phrase: Traceback (most recent call last):

 File "zyLabsUnitTestRunner.py", line 4, in <module>

   from zyLabsUnitTest import test_passed

 File "/home/runner/local/unit_test_student_code/zyLabsUnitTest.py", line 1, in <module>

   from main import get_num_of_characters

 File "/home/runner/local/unit_test_student_code/main.py", line 19, in <module>

   inputStr = input('Enter a sentence or phrase: ')

EOFError: EOF when reading a line'''