Python Forum
Count Letters in a Sentence (without using the Count Function)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count Letters in a Sentence (without using the Count Function)
#1
Hello all,

Newbie Pythoner here.

I have been tasked with Counting Letters in a Sentence. However, I cannot use the Count function. What I have to do is the following:

1) Write a program that asks the user to input a sentence.

2) The program will ask the user what two letters are to be counted.

3) You must use a “for” loop to go through the sentence & count how many times the chosen letter appears in the sentence.

My code is below. As you can see, I used the count function to obtain an understanding of how the function works. My code is still non-functioning.

Anyways, can anyone provide a little guidance of how I can find how to letters can be counted using a For loop and not using the counting function?


sentence = "Hello World"

lettersToFind = 'L', 'E'
count = 0
for INDEX in range (0, len(sentence), 1):
    if sentence[INDEX] == lettersToFind:
       count = count + 1
       print("The", lettersToFind,"were found at index:", INDEX)
print(lettersToFind, "was found", count, "times")
Reply


Messages In This Thread
Count Letters in a Sentence (without using the Count Function) - by bhill - Jun-18-2018, 04:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Count occurences in a string and add to a list using loops Leyo 4 1,692 Mar-11-2022, 03:52 PM
Last Post: Leyo
  an array to count votes thechungusmaximus 2 1,513 Mar-09-2022, 03:44 PM
Last Post: DeaD_EyE
  Using regex to count specific character in file shamishd 1 1,624 Oct-01-2021, 07:33 AM
Last Post: snippsat
  Word count vanjoe198 1 1,974 Mar-16-2021, 12:27 AM
Last Post: BashBedlam
  Convert a sentence to pig latin SalsaBeanDip 4 3,222 Oct-04-2020, 01:45 AM
Last Post: SalsaBeanDip
  sys.stdin to do a word count based on user entry Kaltex 3 3,693 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  Syllable Count Function pav1983 3 2,635 Jun-04-2020, 07:32 AM
Last Post: pyzyx3qwerty
  create a function that can count polk203 1 1,666 Apr-12-2020, 12:13 PM
Last Post: ibreeden
Exclamation A function that takes a word and a string of forbidden letters pooyan89 5 4,680 Jun-12-2019, 09:44 PM
Last Post: nilamo
  count unique letters in a word sunny_awesome 4 8,729 Jun-06-2019, 07:15 PM
Last Post: kotter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020