0

Im unable to count the number of vowels in this program i created. Im unsure why it doesnt work properly

Here is my program

myfile = open("student.txt","r")
ch = ""
vcount = 0
while ch:    
    ch = myfile.read(1)
    if ch in ["a","A","e","E","i","I","o","O","u","U"]:
        vcount +=1
print(vcount)

suppose the contents of student.txt was:

Ethan,Jordan,Kevin

im expecting the program to count the vowel so it should print 6. I tried different read methods and different loops but the result wasnt any better

  • In your own words, where the code says `while ch:` - what do you think will happen the first time this is reached? (Hint: is the code `ch = myfile.read(1)` inside the loop, or outside? Therefore, has it run yet? Therefore, what is the value of `ch`? Will that pass the test?) – Karl Knechtel Jan 12 '23 at 06:23
  • For future questions, please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/, and try to locate problems before asking. "im expecting the program to count the vowel so it should print 6." Okay, so the first thing to ask yourself is what is printed instead. The next thing to ask - since the value of `vcount` is wrong, and the only thing modifying it is `vcount += 1` - is whether that line runs. Working backwards from there reveals the problem: the loop is not entered at all. – Karl Knechtel Jan 12 '23 at 06:33

0 Answers0