Find accurate and reliable answers to your questions on IDNLearn.com. Ask anything and receive well-informed answers from our community of experienced professionals.
Exercise 8-3 Encapsulate
fruit = "banana"
count = 0
for char in fruit:
if char == "a":
count += 1
print(count)
This code takes the word "banana" and counts the number of "a"s. Modify this code so that it will count any letter the user wants in a string that they input. For example, if the user entered "How now brown cow" as the string, and asked to count the w's, the program would say 4.
Put the code in a function that takes two parameters-- the text and the letter to be searched for. Your header will look like def count_letters(p, let) This function will return the number of occurrences of the letter.
Use a main() function to get the phrase and the letter from the user and pass them to the count_letters(p,let) function. Store the returned letter count in a variable. Print "there are x occurrences of y in thephrase" in this function.
Screen capture of input box to enter numbeer 1
Sagot :
We appreciate your contributions to this forum. Don't forget to check back for the latest answers. Keep asking, answering, and sharing useful information. Thank you for choosing IDNLearn.com for your queries. We’re committed to providing accurate answers, so visit us again soon.