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
#2
to get each letter, use:
for letter in sentence:
    ...
remember that a string is a list or characters, so above will get a letter for each iteration.
Reply
#3
The code you have written has almost all the correct elements, but it doesn't work for a few reasons:

1. 'lettersToFind' is a tuple and not a single letter. Ever. So your comparison condition always fails. But Python does provide a dead easy way of fixing this. Since 'lettersToFind' is a tuple, every time you need to check if the letter under consideration is 'in' this data container.
So change line 6 to
if sentence[INDEX] in lettersToFind:
2. The characters in 'Hello World' are of different font cases. For example, if you were to run your code, it would say that 'L' is not found in sentence. That is because it isn't in your sentence. The lowercase version of it is.
So change line 6 to
if sentence[INDEX].upper() in lettersToFind:
3. With 1 and 2, your code will run fine. It's just that the places you are printing out 'lettersToFind' cause me pain. Inside the loop, if found, just print out sentence[INDEX] and its position.

I hope this helped.
Reply
#4
Thank you all! Because of your help, I was able to code an algorithm that worked perfectly!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Count occurences in a string and add to a list using loops Leyo 4 1,637 Mar-11-2022, 03:52 PM
Last Post: Leyo
  an array to count votes thechungusmaximus 2 1,463 Mar-09-2022, 03:44 PM
Last Post: DeaD_EyE
  Using regex to count specific character in file shamishd 1 1,586 Oct-01-2021, 07:33 AM
Last Post: snippsat
  Word count vanjoe198 1 1,939 Mar-16-2021, 12:27 AM
Last Post: BashBedlam
  Convert a sentence to pig latin SalsaBeanDip 4 3,147 Oct-04-2020, 01:45 AM
Last Post: SalsaBeanDip
  sys.stdin to do a word count based on user entry Kaltex 3 3,631 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  Syllable Count Function pav1983 3 2,574 Jun-04-2020, 07:32 AM
Last Post: pyzyx3qwerty
  create a function that can count polk203 1 1,626 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,574 Jun-12-2019, 09:44 PM
Last Post: nilamo
  count unique letters in a word sunny_awesome 4 8,642 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