我有这段代码:
?randomquerystring
并且它不使用首次使用构建器时的当前长度,但它再次从0开始。为什么会这样?
答案 0 :(得分:0)
current_length
不会更改builder
外部功能。但是你会返回新的值,所以就这样做:
current_length = builder(current_lenght,current_value,number_of_bricks,goal)
第一次拨打builder
答案 1 :(得分:0)
关键问题是,您没有将current_lenght
(原文如此)重新分配给builder
正文中函数bricks
返回的值。
我已经解决了这个问题并整理了你的代码风格,使代码更具可读性。我是强迫症,但良好的代码风格和评论也可以帮助捕捉和修复错误。
希望这有帮助!
def bricks(small, big, goal):
# Passing the following values:
# current_length = 0
# current_value = 5
# number_of_bricks = big
current_length = builder(0, 5, big, goal)
print("in bricks", current_length)
# Passing the following values:
# current_length as calculated above
# current_value = 1
# number_of_bricks = small
builder(current_length, 1, small, goal)
if current_length == goal:
return True
else:
return False
def builder(current_length, current_value, number_of_bricks, goal):
use_count = 1
while (goal - current_length) >+ current_value and use_count <= number_of_bricks:
current_length += current_value
print("in while", current_length)
print(current_length)
return current_length
if __name__ == '__main__':
# Let's test what we've written
bricks(1, 10, 100)