1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python统计元音字母个数_计算Python中的元音(Counting vowels in python)

python统计元音字母个数_计算Python中的元音(Counting vowels in python)

时间:2022-02-03 08:47:21

相关推荐

python统计元音字母个数_计算Python中的元音(Counting vowels in python)

计算Python中的元音(Counting vowels in python)

def main():

print(count)

def countVowels(string):

vowel=("aeiouAEIOU")

count=0

string=input("enter a string:")

for i in string:

if i in vowel:

count +=1

main()

为什么它告诉我,当我尝试运行它时,count没有被定义。 我知道这些问题有很多,但我对功能很陌生,可以使用帮助。

def main():

print(count)

def countVowels(string):

vowel=("aeiouAEIOU")

count=0

string=input("enter a string:")

for i in string:

if i in vowel:

count +=1

main()

Why does it tell me that count is not defined when I try to run it. And I am aware that there are multiple of these question, but I am new to functions and could use the help.

原文:/questions/19237791

更新时间:-01-18 13:53

最满意答案

因为count是一个局部变量。 它仅为countVowels函数定义。 另外,你只定义了countVowels函数,但从不运行它。 所以即使在该功能中也不会创建count ...

你可以这样做:

def main(x):

print(x)

def countVowels():

vowels = "aeiouAEIOU"

count = 0

string = raw_input("enter a string:")

for i in string:

if i in vowels:

count += 1

return count

main(countVowels())

这里countVowels返回计数,然后您可以打印它或将其分配给一个变量或做任何你想要的。 你还有一些其他的错误,我在某种方式上修正了......也就是说,函数参数string是无用的,因为你实际上将它作为用户输入。

关于另一个话题,你可以让你的计数更加pythonic:

sum(letter in vowel for letter in string)

另外,在这里我看不到需要创建一个全新的函数来打印结果......只需执行print(countVowels())完成。

另一个改进是只关心小写字母,因为你没有真正区分它们:

vowels = "aeiou"

string = string.lower()

如果不是用户输入,而是想计算给定单词中的元音,则可以这样做(包括上述改进):

def countVowels(string):

vowels = "aeiou"

string = string.lower()

return sum(letter in vowel for letter in string)

print(countVowels("some string here"))

Because count is a local variable. It is defined only for countVowels function. In addition, you only define countVowels function, but never run it. So count is never created even within that function...

You can do this instead:

def main(x):

print(x)

def countVowels():

vowels = "aeiouAEIOU"

count = 0

string = raw_input("enter a string:")

for i in string:

if i in vowels:

count += 1

return count

main(countVowels())

Here countVowels returns the count and then you can print it or assign it to a variable or do whatever you want with it. You also had a couple of other errors I fixed on a way... I.e., the function argument string is useless as you actually take it as a user input.

On another topic, you can make your count a bit more pythonic:

sum(letter in vowel for letter in string)

In addition, here I don't see the need to create a whole new function just to print your result... Just do print(countVowels()) and you're done.

Another improvement would be to care only about lowercase letters, since you don't really distinguish between them:

vowels = "aeiou"

string = string.lower()

If instead of taking user input you'd like to count vowels in a given word you can do it like this (including improvements outlined above):

def countVowels(string):

vowels = "aeiou"

string = string.lower()

return sum(letter in vowel for letter in string)

print(countVowels("some string here"))

-10-08

相关问答

('a' or 'e' ...)行总是计算为'a',这就是letter变量的比较结果。 尝试: if letter in 'aeiou':

...

The ('a' or 'e' ...) line always evaluates to 'a', and that is what the letter variable is being compared to. Try: if letter in 'aeiou':

...

你选择了范围(单词)中的for leter[sic] in word不是单词中的for leter[sic] in word : 在你的python控制台中试试这个: >>> range("word")

Traceback (most recent call last):

File "", line 1, in

TypeError: 'str' object cannot be interpreted as an integer

这是你得到的错误。 这应该修复你的

...

因为count是一个局部变量。 它仅为countVowels函数定义。 另外,你只定义了countVowels函数,但从不运行它。 所以即使在该功能中也不会创建count ... 你可以这样做: def main(x):

print(x)

def countVowels():

vowels = "aeiouAEIOU"

count = 0

string = raw_input("enter a string:")

for i in string:

...

直截了当的方式: 对于文本中的每个字母 如果它是一个元音,打印它 否则打印一个空格 直接转换为Python(以及在打印时将所有内容保持在同一行上的调整): VOWELS = "aeiou"

word = "james is funny"

for letter in word:

if letter in VOWELS:

print(letter, end='')

else:

print(' ', end='')

或者稍微更花哨的方式: 用空格替换

...

添加条件以仅添加具有足够浊度的单词 def vowelCount(s):

vowels = 'aeiou'

countVowels = 0

for letter in s.lower():

if letter in vowels:

countVowels += 1

return countVowels

def manyVowels(t, i):

my_string = t.split()

my_dict =

...

使用sorted或list.sort 。 使用计算元音数量的函数指定key 。 (函数的返回值用作比较键。) 传递reverse=True参数以降序。 >>> word_list = ['banana', 'apple', 'pineapple']

>>> sorted(word_list,

... key=lambda word: sum(ch in 'aeiou' for ch in word),

... reverse=True)

['pineapple', 'b

...

这里有几件事: j.equalsIgnoreCase(“a,e,i,o,u”)将检查j(长度为1的字符串)是否是字符串“a,e,i,o,u”,这几乎肯定不是你想要的(因为它总是假的,因此你为每个辅音设置y = true。 相反,请考虑在每次迭代开始时将布尔值设置为false,并在元音分支中将其设置为true。 然后,如果该变量为真,那么你知道这次你看到了一个元音。 或者只是拥有else分支。 在循环外部将y初始化为false,但是一旦y为真,它就永远不会被重置,因此对于每个字母,您将运行if(y

...

[character for character in sentence if character not in vowels] # returns list

这个列表理解应该为你做的伎俩。 它的工作方式是它利用了vowels和sentences是可迭代的事实:你可以在它们上面定义一个for循环,可以这么说。 具体来说,它会拉出sentence每个字符,检查相同的字符是否没有出现在vowels ,然后才将其插入列表中。 如果你想要一个字符串,只需使用join : ''.join([charact

...

由于我在我的函数中没有使用1字节寄存器,所以这行mov eax, [ecx]应该替换为movzx eax, byte ptr [ecx] 。 byte ptr因为char使用1个字节的存储空间和movzx ,以便用零填充其余的eax 。 #include "stdafx.h"

int lower(int)

{

__asm

{

mov eax, [ebp + 8]

mov ebx, 65

cmp eax, ebx

jn

...

如果你for i in s使用for i in s , i 不是索引 :它是一个字符。 解决此问题的快速方法是使用: for i in range(len(s)-1):

if s[i] in vowels and s[i+1] in consonants:

vowelconscount += 1

elif s[i] in vowels and s[i+1] in vowels:

vowelvowelcount += 1

# ... 这里我们使

...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。