例如,count(「系統錯誤,語法錯誤」,「錯誤」)返回2.我不確定如何編寫此代碼。 任何幫助,不勝感激。 我嘗試啓動代碼,但是在此之後我迷路了。Python:你如何編寫一個函數來計算另一個字符串s1中指定的非重疊字符串s2的出現?
def main():
s1 = input("Please enter string 1: ")
s2 = input("Please enter string 2: ")
print(count(s1,s2))
def count(s1, s2): # define function count that takes two strings as argument
count = 0 # set initial count to 0
while s2 < len(s1):
main()
@neepythonhelp,爲什麼不直接使用內建函數count()。使用s1.count(s2)。 – JkShaw
我想學習如何使用計數器而不是內置函數來做 – needpythonhelp
對不起,我並不真正想要做什麼。你想在while循環的每次迭代中增加一個計數器嗎? –