Python 无法打印全局变量

Python 无法打印全局变量,python,scope,global,Python,Scope,Global,调用函数后,我无法打印最终语句。我在这里尝试过其他解决方案,但似乎无法使读取大小/时钟速率真正全球化 def get_config(): global clock_rate global read_size role = input("Will the Sync Link be used as downlink or uplink? Type \"downlink\" "for downlink,

调用函数后,我无法打印最终语句。我在这里尝试过其他解决方案,但似乎无法使
读取大小
/
时钟速率
真正全球化

def get_config():
    global clock_rate
    global read_size
    role = input("Will the Sync Link be used as downlink or uplink? Type \"downlink\"
                 "for downlink, \"uplink\" for uplink: ")
    if role == "downlink":
        clock_rate = 10240 # Convolutional encoding always disabled when sending to EDU
        print("Configured for downlink mode... Clock rate =",clock_rate,"bits per second.")
    if role == "uplink":
        CE_enabled = input("Is convolutional encoding enabled on the EDU? "
                           "1 is enabled, 0 is disabled: ")
        if CE_enabled == 1:
            read_size = 512 # When convolutional encoding is enabled, the EDU receives
                            # a 512 byte CADU frame.
            print(read_size)
            clock_rate = 20480
        if CE_enabled == 0:
            read_size = 256 # When convolutional encoding is disabled, the EDU receives
                            # a 256 byte CADU frame.
            clock_rate = 10240
        else:
            print("Invalid input")

get_config()
print("Initiating transmit/receive... Read size =", read_size,
      "bytes. Clock rate =", clock_rate, "bits per second.")

即使忽略对全局变量的依赖,您也没有在所有可能的情况下定义
时钟速率
读取大小
。执行以下操作:

def get_config():
    clock_rate = None
    read_size = None
    role = input("Role?")
    if role == "uplink":
        clock_rate = 10240
    elif role == "downlink":
        ce_enabled = input("Convolutional encoding enabled?")
        if ce_enabled == "0":
            read_size = 256
            clock_rate = 10240
        elif ce_enabled == "1":
            read_size = 512
            clock_rate = 20480
        else:
            raise ValueError("Invalid response for convolutional encoding")
    else:
        raise ValueError("Invalid role")
    return clock_rate, read_size


clock_rate, read_size = get_config()

print("Initiating transmit/receive... Read size =",read_size,"bytes. Clock rate =",clock_rate,"bits per second.")

您可以为每个变量提供默认值(以前指定的
None
)而不是在每种情况下都引发错误。

我将把它作为一个答案而不是注释,以防止出现任何混淆。要使用全局变量,您不必在全局范围内初始化它们。以下代码将完美地工作:

def some_function():
    global some_global
    some_global = "Hello, world!"

some_function()
print(some_global)
全局变量不必在外部范围内初始化。相反,它们可以在任何时间在任何范围内定义。唯一的限制是:在访问它们之前必须定义它们。这就是你的代码的问题所在


在“下行链路”模式下,您的函数不定义
read_size
,在“上行链路”模式下,您将输入与
0
1
进行比较,它们是整数,因此这些表达式永远不会产生
True
。这意味着,到达告诉用户输入无效的
else
块。在这种情况下,无论是
read\u size
还是
clock\u rate
都不会在最终打印语句之前定义。

在使用全局关键字修改其范围之前,您需要声明clock rate和read\u size变量,以便更改它们

您还需要一个elif CE_enabled==0,否则如果您输入1,您总是会得到无效的输入

见下文:

clock_rate=None
read_size = None

def get_config():
    global clock_rate
    global read_size
    role = input("Will the Sync Link be used as downlink or uplink? Type \"downlink\" for downlink, \"uplink\" for uplink: ")
    if role == "downlink":
        clock_rate = 10240 # Convolutional encoding always disabled when sending to EDU
        print("Configured for downlink mode... Clock rate =",clock_rate,"bits per second.")
    if role == "uplink":   
        CE_enabled = int(input("Is convolutional encoding enabled on the EDU? 1 is enabled, 0 is disabled: "))
        if CE_enabled == 1:
            read_size = 512 # When convolutional encoding is enabled, the EDU receives a 512 byte CADU frame
            print("Read size:", read_size)
            clock_rate = 20480
        elif CE_enabled == 0:
            read_size = 256 # When convolutional encoding is disabled, the EDU receives a 256 byte CADU frame
            clock_rate = 10240
        else:
            print("Invalid input")

get_config()

print("Initiating transmit/receive... Read size =",read_size,"bytes. Clock rate =",clock_rate,"bits per second.")

您需要在函数范围之外定义
clock\u rate
read\u size
。为什么不让函数返回
read\u size
clock\u rate
呢?我已经尝试在函数之外定义变量,但我仍然无法让read\u size工作。我可以这样做,但这需要我重写更多的函数。我强烈建议重写更多的函数。函数的执行可能会也可能不会为这两个变量赋值——因此,如果要在调用后引用它们的值,则需要确保它们存在,并且在调用之前具有一些默认值它。这根本不是真的,看我的答案。谢谢你,我用了这个,它成功了。对于您的原始代码,我使用的API有一个错误,但在将clock_rate=None和read_size=None放入get_config()后,我修复了它。非常感谢你的帮助。