用于python结构自动化中的循环

用于python结构自动化中的循环,python,fabric,Python,Fabric,如何使用循环内部结构定义 expample: Create number of ipspace: ips_total=raw_input("Enter numper of ipspace required:") for x in range(1,ips_total): print"network ipspace create -ipspace ipspace{}".format(x) 在织物def内部,我希望用于循环或任何其他方式来实现这一点 当用户输入4时说 fr

如何使用循环内部结构定义

expample:


  Create number of  ipspace:

  ips_total=raw_input("Enter numper of ipspace required:")
  for x in range(1,ips_total):
  print"network ipspace create -ipspace ipspace{}".format(x)
在织物def内部,我希望用于循环或任何其他方式来实现这一点

当用户输入4时说

  from fabric.api import *
  def ipspace():
      run("network ipspace create -ipspace ipspace1",shell=False)
      run("network ipspace create -ipspace ipspace2",shell=False)
      run("network ipspace create -ipspace ipspace3",shell=False)
      run("network ipspace create -ipspace ipspace4",shell=False)

因为我们不知道用户输入的数字,所以最好在结构内部使用循环逻辑。任何人都可以帮助实现这个

那么,我可以稍微更改一下ipspace吗,这样它就可以多次运行作为参数了

from fabric.api import *
def ipspace(n):
    for i in range(n):
        run("network ipspace create -ipspace ipspace" + str(i+1),shell=False)

嗨,Jefferson,def ipspace(n):不工作我使用了ipspace(),所以它工作得非常好。如果你想从变量中读取,你应该以某种方式将它传递给函数。我尝试使用一个参数,这反过来意味着所有调用方都应该被更改为将
n
赋给
ipspace
。另一种方法是声明这是一个对象的方法,该对象具有一些
n
作为属性,或者您可以使用
n
作为全局资源,以便可以从
ipspace
和读取用户输入的点访问它