我正在制作一个程序,要求用户输入朋友的姓名,然后询问用户的姓名。然后告诉他们名字中有多少个元音,但我的代码只计算其中一个单词中的元音数:
vowel_list = ["a","e","i","o","u"]
friend_name = input("enter your friends name")
yourname = input("enter your name")
def awesome(yourname,friend_name):
vowel_count = 0
friend_name = list(friend_name)
yourname = list(yourname)
for i in range(len(yourname)):
if yourname[i] in vowel_list:
vowel_count += 1
return vowel_count
for i in range(len(friend_name)):
if me[i] in vowel_list:
vowel_count += 1
return vowel_count
print(awesome(yourname,friend_name))
答案 0 :(得分:0)
您的函数中有两个return
语句。第一个之后的所有代码永远不会被执行。
只需为一个名称编写一个函数:
vowels = set(["a","e","i","o","u"])
def awesome(name):
vowel_count = 0
for letter in name:
if letter in vowels:
vowel_count += 1
return vowel_count
并将其用于两个名称:
friend_name = input("enter your friends name")
yourname = input("enter your name")
print(awesome(friend_name))
print(awesome(yourname))