无法打印全局变量

Not able to print global variables

我无法在调用函数后打印最终语句。我从这里尝试了其他解决方案,但似乎无法使 read_size/clock_rate 真正成为全球性的。

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.")

即使忽略对全局变量的依赖,您也没有在所有可能的情况下定义 clock_rateread_size。执行如下操作:

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),而不是在每种情况下都引发错误。

我将把它作为一个答案而不是评论来防止任何混淆的发生。要使用全局变量,您 NOT 必须在全局范围内初始化它们。以下代码将完美运行:

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

some_function()
print(some_global)

Globals not 必须在外部范围内初始化。相反,它们可以随时在任何范围内定义。唯一的限制是:它们必须在访问 之前定义。这就是您的代码有问题。

在 'downlink' 模式下,您的函数未定义 read_size,而在 'uplink' 模式下,您正在将输入与 0 和 [=13= 进行比较],它们是整数,因此这些表达式永远不会导致 True。这意味着,到达 else 块,告诉用户输入无效。在这种情况下,read_sizeclock_rate 都不会在最终打印语句之前定义。

在使用 global 关键字修改它们的范围之前,您需要声明时钟频率和 read_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.")