【Python】 Count the different vowels in a string. 不同元音数量

本文介绍了一种计算输入字符串中元音字母总数的方法,并提出了如何确定不同元音字母的数量,即使重复出现也只计数一次的挑战。示例展示了各种输入情况下的预期输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 19 12:37:04 2019

@author: shark
"""

print("This code should compute the number of vowels in a string.")

input_string   =  input( "Enter a string (e.g. your name): " ) 

a = "a"
A = "A"
e = "e"
E = "E"
i = "i"
I = "I"
o = "o"
O = "O"
u = "u"
U = "U"

acount = 0
ecount = 0
icount = 0
ocount = 0
ucount = 0


if a or A in input_string :
     acount = acount + 1
if e or E in input_string:
     ecount = ecount + 1
if i or I in input_string :
     icount = icount + 1
if o or O  in input_string :
     ocount = ocount + 1
if u or U in input_string :
     ucount = ucount + 1

number_of_vowels = acount+ecount+icount+ocount+ucount

print( "The number of vowels in the string is:", number_of_vowels )

 

 

(b) Number of different vowels in a string

To answer this question you need to write code that will determine the number of different vowels in a string input by the user. By "different vowels" we mean that if the same vowel occurs more than once it is only counted once. Moreover, small and captial versions of the same letter do NOT count as different vowels. Thus, the number given as the answer will always be from 0 to 5 (inclusive).

 

Examples
INPUTOUPUT
yyyykz0
A cat sat on a mat2
Abracadabra1
Eutopia5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值