Python exec() 返回 None

标签 python

当使用 exec() 执行字符串时,它返回 None。

tree_1= '''def tree():
  input_user = input('elektriciteit | Y&N')
  if input_user == 'Y':
    return 1800000
  else:
    input_user = input('accelerator | Y&N')
    if input_user == 'Y':
      return 2170701
    else:
      return 2170705
tree()'''


output = exec(tree_1)
print(output)

如何将返回值保存到变量中。评估不起作用。

最佳答案

刚刚找到答案。

tree_1= '''def tree():
  input_user = input('elektriciteit | Y&N')
  if input_user == 'Y':
    return 1800000
  else:
    input_user = input('accelerator | Y&N')
    if input_user == 'Y':
      return 2170701
    else:
      return 2170705
result = tree()'''

exec(tree_1)

global result
print(result)

关于Python exec() 返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50817634/

相关文章:

python - 用于身份验证和登录的 Webapp2

python - 合并两个字典的字典(Python)

python - 将我的 python 代码转换为 Windows 应用程序(右键单击菜单)

python - 将 KNN 火车从 Opencv 3 转换为 2

python 一次读取 1000 直到文件结束?

python - python编程时出现Elif错误

python - 在 Python 中保存文件时,我应该将连接的文件路径放在哪里?

Python re.sub() : how to substitute all 'u' or 'U' s with 'you'

python - 在 Python 中使用扩展 Ascii 代码

python - 如何删除/root/.local/lib/python2.7?