-1

Write a while loop that takes a string and counts the vowels. Use the string “May the force be with you.” Print the results. (Answer: 8)

Any help with this would be greatly appreciated! I kept coming up with a continuous loop. It has to use a while loop and print the number of vowels (8). Thank you!!

count = 0
vowels = ['a', 'e', 'i', 'o', 'u']
s = "May the force be with you."
while i in s:
    if i in vowels:
        count += 1
print(count)
progbilly
  • 11
  • 1

3 Answers3

1

The statement:

while i in s:

does not do what you think. Were that while a for, it would iterate over the string one character at a time, and probably work.

However, the expression i in s (which is what that is in a while statement) simply checks if i is one of the things in the s "collection" and gives you true or false. It does not iterate i over the s collection.

If i had been set to something, the while loop would either execute infinitely or never, depending on the value of i. If i is not bound to a value, you'll get a run-time error.


As a solution, you can iterate over the characters in a string with something like (from an actual transcript):

>>> str = "pax"
>>> for ch in str:
...     print(ch)
...
p
a
x

The equivalent while version would be:

>>> str = "pax"
>>> idx = 0                 # OR: idx, slen = 0, len(str)
>>> while idx < len(str):   #     while idx < slen:
...     print str[idx]
...     idx += 1
...
p
a
x

though the for variant is generally considered more Pythonic for this sort of task.

Further, you can detect if a character is one of a set of characters by using in, such as in the following transcript:

>>> str = "pax"
>>> for ch in str:
...     if ch in "ABCabc":
...         print(f"{ch} is either a, b, or c")
...
a is either a, b, or c

So you should be able to combine that for/while loop and if statement to count the vowels and output it (with print).

Note especially the string I use for vowel checking, it also contains the upper-case vowels. And keep in mind, though your specification may only be to use the Latin-ish vowels, the Unicode world of today would not forgive this oversight. See here for example.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
-1

I think you could use something like this

phrase = "May the force be with you."

vowels = 0
count = 0

while phrase[count] != ".":
  if phrase[count] in 'aeiou':
    vowels += 1
  count+=1

print(vowels)
-1

Since it needs to be a while, you can slice off a character at a time and loop until the string is empty. As a quick optimization, True adds as 1 and False as 0, so the interior if can be removed.

vowels = ['a', 'e', 'i', 'o', 'u']
s = "May the force be with you."
count = 0
while s:
    count += s[0] in vowels
    s = s[1:]
print(count)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Not the person who downvoted, but I'd imagine that it was downvoted because although it's technically correct, it's not very Pythonic. [PEP 20](https://www.python.org/dev/peps/pep-0020/) has "Explicit is better than implicit", which I'd argue `count += s[0] in vowels` is not explicit. Additionally, if optimization is the concern - an implementation of the top answer by @paxdiablo is faster than your code by ~1µs on my machine. – Seabody Aug 26 '20 at 04:51
  • @Seabody - I appreciate the observation, but it is pythonic. The [doc](https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy) says _The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1_. And its common to count truthy things as for instance `sum(val == 'foo' for val in may_be_foo)`. Some say that's "the canonical way" but, eh, I think that's going a bit far. – tdelaney Aug 26 '20 at 05:10
  • Don't get me wrong, I'm not arguing it's not valid or a bad way to code - I've used True as 1 and False as 0 many times in my life. It's just my 2 cents on why it may have been downvoted given that whomever did so didn't leave feedback. – Seabody Aug 26 '20 at 05:14
  • @Seabody - I know what you mean. I'm toying with changing the code. – tdelaney Aug 26 '20 at 05:16
  • If you're going to change the code, I'd probably get rid of the inefficiency of constructing a new string in every iteration. – paxdiablo Aug 26 '20 at 06:51