【发布时间】:2020-11-12 07:22:00
【问题描述】:
make_substring(chars: str, start: int, stop: int, step: int)-> str
想要一种方法,它返回给定字符串的子字符串,该子字符串从开始索引(包括)开始,到停止索引(不包括)结束,每次迭代都会增加步骤字符。假设 start 和 stop 是非负的,而 step 是正的。也使用 for 循环。
def make_substring(chars: str, start: int, stop: int, step: int) -> str:
c = chars
s = ""
if start + step < stop:
for i in range(start, stop, step):
s = c[start] + c[start + step]
return s
# using test case:
make_substring("ABCD", 0, 3, 2) == "AC"
【问题讨论】:
-
您不觉得在
for循环中没有使用i很可疑吗?