mystery_string = "Hello! What a fine day it is today." mystery_character = "a" #----------------------------------------------------------- #You may modify the lines of code above, but don't move them! #When you Submit your code, we'll change these lines to #assign different values to the variables. #Write some code below that will count and print how many #times mystery_character appears in mystery_string. You may #not use the string class's .count method. # #With the original values for mystery_string and #mystery_character, your code should initially print 4. Only #count characters with the same case as mystery_character #(in other words, here you would ignore capital A). #Add your code here!

Respuesta :

Answer:

Here is the Python code:

mystery_string = "Hello! What a fine day it is today."

mystery_character = "a"

counter = 0

for i in range(len(mystery_string)):  

   if (mystery_string[i] == mystery_character):

       counter+=1

print (counter)  

                             

Explanation:

The above program has two variables mystery_string which contains the string Hello! What a fine day it is today and mystery_character which contains character a. The program counts and prints the number of times character a appears in the string.

It has a counter variable to store the number of times mystery_character appears in the mystery_string.

The loop has a variable i that moves through the mystery_string character by character and the loop continues to execute till i reaches the length of the mystery_string. If the character at position i is equal to the mystery_character, then counter variable counts it and increments its value to 1 each time the mystery_character is found in the mystery_string.

When the loop terminates it finally returns the number of times mystery_character appears in the mystery_string.

The screenshot of the program along with its output is attached.

Ver imagen mahamnasir