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)
#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


Messages In This Thread
RE: Count Letters in a Sentence (without using the Count Function) - by cryomick - Jun-18-2018, 07:58 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,776 Mar-11-2022, 03:52 PM
Last Post: Leyo
  an array to count votes thechungusmaximus 2 1,575 Mar-09-2022, 03:44 PM
Last Post: DeaD_EyE
  Using regex to count specific character in file shamishd 1 1,682 Oct-01-2021, 07:33 AM
Last Post: snippsat
  Word count vanjoe198 1 2,029 Mar-16-2021, 12:27 AM
Last Post: BashBedlam
  Convert a sentence to pig latin SalsaBeanDip 4 3,305 Oct-04-2020, 01:45 AM
Last Post: SalsaBeanDip
  sys.stdin to do a word count based on user entry Kaltex 3 3,788 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  Syllable Count Function pav1983 3 2,730 Jun-04-2020, 07:32 AM
Last Post: pyzyx3qwerty
  create a function that can count polk203 1 1,725 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,835 Jun-12-2019, 09:44 PM
Last Post: nilamo
  count unique letters in a word sunny_awesome 4 8,854 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