规范:读入字符,直到用户输入句号"."。显示小写元音的数目。
到目前为止,我已经成功地完成了读取循环并打印出"元音计数:"。然而,元音计数总是为0。我刚开始。我正在为"显示小写元音的数量"的位置而苦苦挣扎我应该定义元音=。。。在顶部?或者稍后将其循环?我要创建一个新循环吗?我一直没能让它发挥作用。感谢
c = str(input('Character: '))
count = 0
while c != '.':
count += 1
c = str(input('Character: '))
print("Vowel count =", count)
以下是您的问题的解决方案。您应该在代码中添加2个条件。检查字符是否为元音,是否为小写:
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)
- 在
while
循环中,您可以使用字符串方法islower()
添加if
条件来检查字母是否为小写,并且字母是否为元音。如果两个条件都满足,则将计数增加1 - 您不必将输入字符串转换为
str
类型,因为输入字符串总是字符串类型 - 此外,在while循环之外,您可以将变量声明为空字符串
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)
小写元音对应于ascii表中的一组数字,大小写也适用于'.'
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)