Home > Blockchain >  Python: 2 Conditions - Read in characters Until x, and Count Vowels
Python: 2 Conditions - Read in characters Until x, and Count Vowels

Time:08-24

Specification: Read in characters until the user enters a full stop '.'. Show the number of lowercase vowels.

So far, I've succeeded in completing the read loop and printing out 'Vowel Count: '. However, vowel count always comes to 0. I've just started. I'm struggling with placement for 'Show the number of lowercase vowels' Should I define vowels = ... at the top? Or put it in a loop later? Do I create a new loop? I haven't been able to make it work. Thanks

c = str(input('Character: '))
count = 0

while c != '.':
        count  = 1
        c = str(input('Character: '))

print("Vowel count =", count)

CodePudding user response:

Here is the solution of your problem. You should add 2 conditions in your code. You check If the character is vowel and if is lowercase:

c = str(input('Character: '))
count = 0

while c != '.':
        if c in 'aeyio' and c.islower():
            count  = 1
        c = str(input('Character: '))

print("Vowel count =", count)

CodePudding user response:

Lowercase Vowels correspond to a set of numbers in the ascii table, same case applies to the '.'

c = str(input('Character: '))
count = 0
lowerCaseVowel=[97,101,105,111,117]

for _ in c:
## check for .
    if ord(_) ==46:
        break
    if ord(_) in lowerCaseVowel:
        count  =1

print("Vowel count =", count)

CodePudding user response:

  • In while Loop you can add if condition to check if letter is in lower case using string method islower() and is letter is vowel. if both condition satisfy then increase count by 1.
  • You don't have to convert input string into str type since input string is always in string type.
  • Also, outside while loop you can declare variable as empty string.

c =''
count = 0

while c != '.':
        c = input('Character: ')
        if c.islower() and c in ('a','e','i','o','u'):
            count  = 1

print("Vowel count =", count)
  • Related