变量没有被改变python 3.6

时间:2018-06-07 20:46:58

标签: python python-3.x

在调用Centos7时,变量不会改变。

代码

#!/usr/bin/env python

import subprocess

#variables will be imported from chef.

Desired_os='Centos7'
clonedvm='default'
New_Vm_Name = 'banana'


#function descriptors and dictionary (al this for a fucking switch statement?)

def Centos7():
    clonedvm='CentosCloneMe'
    print ("cloning Centos")
def debian():
    print("debian not yet implemented")
def manjora():
    print("manjora not yet implemented")
def ubuntu18():
    print("ubuntu18 not yet implemented")


options =  {
            'Centos7' : Centos7,
            'Debian' : debian,
            'ubuntu18' : ubuntu18,
            'manjora' : manjora,
}
#---------------------------------------------------------------------------------end of reusable switch


#enter data in switch statement
options[Desired_os]()

args = f"virt-clone -o {clonedvm} -n {New_Vm_Name} --auto-clone"

subprocess.call(args, shell=True)

错误

[drunkenpanda@Diablo Scripts]$ sudo ./clonevm.py 
[sudo] password for drunkenpanda: 
cloning Centos
ERROR    Domain 'default' was not found.

此错误表示未找到名为default的vm,但变量clonedvm应更改为'CentosCloneMe'

1 个答案:

答案 0 :(得分:0)

您的函数Centos7正在创建一个名为clonedvm的局部变量,该变量将隐藏外部作用域中的变量。尝试全球化

def Centos7():
    global clonedvm
    clonedvm='CentosCloneMe'
    print ("cloning Centos")

免责声明:全局变量错误。试着找另一种方式。