s= str(raw_input("Enter a string of characters:"))
m =0
def numvow(s,m):
for m in s:
if m == 'a'or m =='e' or m =='i' or m =='o' or m =='u':
m+=1
print("The number of vowels is " +str(m))
s= str(raw_input("Enter a string of characters:"))
c =0
def numvow(s,c):
for m in s:
if m == 'a'or m =='e' or m =='i' or m =='o' or m =='u':
c+=1
return c
c = numvow(s,c)
print("The number of vowels is " +str(c))
s = raw_input("Enter a string of characters:")
m = 0
def numvow(s,m):
for c in s:
if c == 'a' or c =='e' or c =='i' or c =='o' or c =='u':
m+=1
return m
m = numvow(s, m)
print("The number of vowels is " +str(m))
首先,您不需要在这里使用函数,因为它实际上并不能为您带来任何好处。其次,您将使用名称
m
来表示两个不同的变量。你知道吗还要注意我是如何用一个简单的
in
检查来替换单调乏味的if m == 'a' or m =='e' ...
。你知道吗你有很多错误
m
对程序进行最小限度的编辑
一些提示
format
连接"The number of vowels is {}".format(c)
raw_input
返回str
,因此强制转换是多余的if m in ('a','b','c','d','e'):
它又短又简单你丢失了一个
return m
,而且你从不调用numvow
;另外,你在整个过程中将m
重新分配给s
中的单个字符,而不是像你应该的那样使用一个单独的循环变量。所以,有三个很好的理由工作:-). 易于修复:我做了另一个改进,删除了对
str
的一个冗余调用(因为raw_input
已经返回了一个字符串,没有理由这样调用),但其他调用还没有,比如将if
更改为更简洁、更快速的调用相关问题 更多 >
编程相关推荐