嘗試訪問此字符串以測試它是否在其中有3個或更多藍調「b」。 ---測試和three_or_more_blues都是函數。-----我完全失去了,任何人都有想法?如果它不符合我的問題,請更改我的標題。不確定如何問這個問題。謝謝!如何訪問我的函數python中的字符串?
test(three_or_more_blues, "brrrrrbrrrrrb")
嘗試訪問此字符串以測試它是否在其中有3個或更多藍調「b」。 ---測試和three_or_more_blues都是函數。-----我完全失去了,任何人都有想法?如果它不符合我的問題,請更改我的標題。不確定如何問這個問題。謝謝!如何訪問我的函數python中的字符串?
test(three_or_more_blues, "brrrrrbrrrrrb")
您可以使用.count()。
sentence = 'brrrrrbrrrrrb'
amount = sentence.count('b')
print(amount)
然後你可以使用一個循環來計算你的下一步。
if (amount >= 3):
# Do something
假設test
是一個函數,它的功能和一個字符串作爲paramters,和three_or_more_blues
是如果字符串參數具有3個或更多的「B」的字符,則返回true的函數,然後
def test(func, str):
if func(str):
# do something with str
test(three_or_more_blues, "brrrrrbrrrrrb")
我不確定我是否正確理解你 - 你在問如何將字符串'brrrrrbrrrrrb'傳遞給three_or_more_blues
函數?
如果是這樣的話,比你只是單純地通過它,當你調用three_or_more_blues功能是這樣的:
def test(func, some_string):
func(some_string) # here you call the passed function
# if three_or_more_blues would look like this:
def three_or_more_blues(some_string):
print "Yes, 3 or more b's" if some_string.count('b') >= 0 else "No"
# you would get this from your function call
test(three_or_more_blues, "brrrrrbrrrrrb") # prints: "Yes, 3 or more b's"