對於下面的代碼,在運行start_point = start_point/10
之後,它的值降至1,000,並且如果隨後調用它,它將保持原樣。Python:重置變量
我可以知道如何將它重設爲原來的10,000?我相信一種方法是設置一個全局變量。但有沒有更簡單的方法?像seek(0)
?
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans/1000
crates = jars/100
return jelly_beans, crates, jars
start_point = 10000
item_1, item_2, item_3 = secret_formula(start_point)
print "With a starting point of: %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (item_1, item_2, item_3)
start_point = start_point/10
print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
爲什麼你甚至保存新的價值,如果你打算在之後重置它呢?爲什麼不只是'打印'我們會有%d豆,%d罐和%d箱。「 %secret_formula(start_point/10)'而不是? –
謝謝你指出。應該做到這一點! – ferry