相关疑难解决方法(0)

在函数内部使用exec设置变量

我刚开始自学Python,我需要一些关于这个脚本的帮助:

old_string = "didnt work"   
new_string = "worked"

def function():
    exec("old_string = new_string")     
    print(old_string) 

function()
Run Code Online (Sandbox Code Playgroud)

我想得到它old_string = "worked".

python string variables function exec

21
推荐指数
2
解决办法
1万
查看次数

标签 统计

exec ×1

function ×1

python ×1

string ×1

variables ×1