why is my command block for identifying vowels in text not showing?

90 views Asked by At
text = """
Inserting comments into program code is normal practice.
As a result, every programming language has some way of allowing comments to be inserted into programs.
The objective is to add descriptions to parts of the code, either to document it or to add a description of the implemented algorithm."
"""

vowel = 'a' or 'e'
for i, vowel in enumerate(text):
    if vowel == text:
       print(f"Vowel {vowel} found in position {i}")
    else:
       continue

I am trying to create this block where it should show which of the vowels was found and its position in the text, however, when I run the program it doesn't execute anything. I started recently and I'm trying to understand why it's not working...

3

There are 3 answers

1
OysterShucker On

You have 2 problems.

  1. vowel will always start as 'a', and never even get to the or 'e' part.
  2. you are overwriting vowel on every iteration of the loop

After fixing the issues, turn your logic into a function and store the results in a more useful format. The below is an example.

from json import dumps

#set to False to individually toggle dump
MARKCHARS_DUMPALL = True 

def mark_chars(data:str, chars:str, dump:bool=False) -> dict:
    # dict of lists for each character to find
    report = {c:[] for c in chars}
    
    # upgrade to a faster iterable
    chars  = set(chars)
    
    #for every character in data
    for i, char in enumerate(data):
        # if char is a character we are looking for, record it's index 
        if char in chars:
            report[char].append(i)
          
    # if any kind of dumping is True, prettyprint report in console          
    if dump or MARKCHARS_DUMPALL: 
        print(dumps(report, indent=4))
            
    # return results
    return report

use

text = """
Inserting comments into program code is normal practice.
As a result, every programming language has some way of allowing comments to be inserted into programs.
The objective is to add descriptions to parts of the code, either to document it or to add a description of the implemented algorithm."
"""

marks = mark_chars(text, 'ae')

output

{
    "a": [
        30,
        45,
        50,
        61,
        82,
        90,
        94,
        99,
        108,
        114,
        157,
        182,
        203,
        249,
        253,
        286
    ],
    "e": [
        4,
        15,
        36,
        55,
        64,
        71,
        73,
        96,
        105,
        127,
        136,
        141,
        144,
        164,
        169,
        174,
        187,
        213,
        218,
        221,
        225,
        236,
        256,
        272,
        278,
        280,
        283
    ]
}
0
toyota Supra On

I am trying to create this block where it should show which of the vowels was found and its position in the text, however, when I run the program it doesn't execute anything.

The problem can be fixed.

Change == to in.

On line 9, change this:

if vowel == text:

to:

if vowel in text:
0
michaelgbj On

just focus on this:

for i, vowel in enumerate(text):
    if vowel == text:

you are comparing every letter in your text against the entire text, of course it is never the same.

I assume you want this:

vowels = ['a', 'e', 'i', 'o', 'u']
for i, letter in enumerate(text):
   if letter in vowels: