如何在python中的函数内创建模块级全局变量

沙比布·阿斯加尔

我有一个名为functions的文件在这个文件中,我有以下代码的功能

def converter(a):
    index = a.find(',')
    b = a[0:index]
    d = a[index + 1:]
    c1_1 = b.strip()
    c1_2 = d.strip()

现在我想将该函数导入另一个文件,然后我想对 c1_1 和 c1_2 变量应用一些条件,例如

from functions import converter
h = input("Type a command: ")
converter(h)
if c1_1 == 'a'
    print('something')

我已经尝试过这种方法,但它不起作用

致癌物

这不会直接回答您的问题,但您应该只返回值而不是依赖全局变量:

def converter(a):
    index = a.find(',')
    b = a[0:index]
    d = a[index + 1:]
    c1_1 = b.strip()
    c1_2 = d.strip()

    return c1_1, c1_2  # Return the values from the function 

. . . 

h = input("Type a command: ")
c1_1, c1_2 = converter(h)  # Then assign them here
if c1_1 == 'a' print('something'):

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章