大家下午好,
我是一个Python初学者,我正在尝试实现以下脚本来生成自动输入机器的命令列表。脚本现在看起来像:
print("re 1")
make_same = "xfb nc_proc 0"
first_exp = 1
exp_id = str(first_exp)
copy_par = "rpar re999"
next_please = "re " + exp_id
def macro_maker():
parameters = [copy_par, make_same, next_please]
for settings in parameters:
print(settings)
macro_maker()
def repeat(f, n):
for i in range(n):
f()
repeat(macro_maker, 1)
我想对其进行修改,使其无需我的输入即可自动传递到下一个实验。例如,当前输出为:
re 1
rpar re999
xfb nc_proc 0
re 1
rpar re999
xfb nc_proc 0
re 1
虽然我想要以下输出:
re 1
rpar re999
xfb nc_proc 0
re 2
rpar re999
xfb nc_proc 0
re 3
我尝试了好几天查找类似的问题,但每次都只能提出新的错误!
非常感谢任何可以帮助我完成这项任务的人。
最佳答案
问题仅在于您的 next_please
字符串在整个脚本中保持不变。因此它总是打印相同的内容're 1'
。
据我了解,这是唯一要更改的参数,您可以做的是:
make_same = "xfb nc_proc 0"
copy_par = "rpar re999"
next_please = "re {}"
def macro_maker(index):
print(next_please.format(index))
parameters = [copy_par, make_same]
for settings in parameters:
print(settings)
def repeat(f, n):
for i in range(1, n+1):
f(i)
repeat(macro_maker, 2)
macro_maker
采用实验索引,并以不同于所有其他参数的方式对待 next_please
,向其提供当前索引以格式化模板字符串"re {}"
.
关于python - 实验设置中的自动化脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55138036/