我们进行了此python代码练习,当它运行时,print(loop(-1,-2,0))的预期输出应为-1时返回空。谁能帮忙,如何改进此代码以返回-1输出?非常感谢你。
def loop(start, stop, step):
return_string = ""
if step == 0:
step = 1
if step > 1:
step = abs(step) * -1
else:
step = abs(step)
print(step)
for count in range(start, stop, step):
return_string += str(count) + " "
return return_string.strip()
答案 0 :(得分:0)
这对我有用:
def loop(start, stop, step):
return_string = ""
if step == 0:
step = 1
if start > stop:
step = abs(step) * -1
else:
step = abs(step)
for count in range(start, stop, step):
return_string += str(count) + " "
return return_string.strip()