日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/9 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.

原文:https://stackoverflow.com/questions/19237791

更新時間:2020-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"))

2013-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

# ... 這里我們使

...

總結

以上是生活随笔為你收集整理的python统计元音字母个数_计算Python中的元音(Counting vowels in python)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。