[英]Python: Trying to run a function multiple times and save values for each run
[英]When running a function multiple times how do i save the data after each time it is run?
我已经尝试过很多次了,但是每次运行后,我所做的变量仍然设置为 0,但在最后一次运行后,它设置为 1,这是一种方法吗? 到目前为止,这是我的代码。
我如何做到这一点,每当它在下一次循环时,数量的值不会为 0
编辑1:新代码:
global amount, penny, pebble, rock, bottle, cap, ten_dollar_note, percentage
pebble = 0
rock = 0
bottle = 0
cap = 0
penny = 0
ten_dollar_note = 0
amount = 0
percentage = random.randint(0, 100)
if percentage <= 10:
ten_dollar_note += 1
amount += 1
elif percentage <= 35:
rock += 1
amount += 1
elif percentage <= 45:
pebble += 1
amount += 1
elif percentage <= 65:
cap += 1
amount += 1
elif percentage <= 85:
penny += 1
amount += 1
random_percentage = random.randint(15,31)
times = 0
while times != random_percentage:
overworld_choose()
times += 1
amount_one = amount```
现在的问题是每个循环我想在它之前添加 function 的值所以我尝试做 amount_one = amount + 0 但它似乎不起作用
我修复了它,我所要做的就是在函数之外添加一个名为 new_amount 的变量,并将其设为 while 循环:
while times != random_percentage:
overworld_choose()
times += 1
amount1 = amount
new_amount += amount```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.