我試圖找到遞回在低和高指數的決定被認為是其中的字串需要部分的字串的小寫字母數:def count_lowercase(s, low, high)。一旦遇到大寫字母或空格,我的代碼就會停止遍歷字串,if s[count] == s[count].lower() and s[count] != ' '但我無法弄清楚如何讓它繼續。任何幫助是極大的贊賞!
我的代碼
def count_lowercase(s, low, high):
def countfunc(s2=s[low: high 1], count=0):
if count > high:
return count
else:
if s[count] == s[count].lower() and s[count] != ' ':
return countfunc(s2, count 1)
return count
return countfunc()
uj5u.com熱心網友回復:
如果您試圖在字串的一部分中找到小寫字母,為什么不遵循以下方法:
import re
def count_lowercase(s, low, high):
return len(re.findall("[a-z]", s[max(0,low):min(high, len(s))]))
跟進:如果您不能使用任何模塊:
def count_lowercase(s, low, high):
s = s[max(0,low):min(high, len(s))].strip(" ")
return sum([x==x.lower() for x in s])
uj5u.com熱心網友回復:
在有大寫或空格沒有處理的情況下,如果不是小寫,也不是空格,則立即回傳計數。
不要使用計數作為索引,而是將其分開。
def count_lowercase(s, low, high):
def countfunc(s2=s[low: high 1], count=0, index=0):
if index > high:
return count
else:
if s[index] == s[index].lower() and s[index] != ' ':
return countfunc(s2, count 1, index 1)
else:
return countfunc(s2, count, index 1)
return count
return countfunc()
想想如果代碼到達一個字母是空格或大寫,你的 if 陳述句是錯誤的并且沒有被處理,直接轉到return count. 如果您輸入一個else用于處理大寫/空格并仍然使用計數作為索引,則即使到達大寫字母或空格,它也會增加到下一個索引。
將它們分開并添加其他!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/335442.html
上一篇:中職網安 命令注入 筆記
下一篇:java資料安全
