简体   繁体   中英

Not able to print global variables

I am not able to print the final statement after the function is called. I have tried other solutions from here, but can't seem to get read_size / clock_rate to be truly 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, \"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.")

Even ignoring the dependence on global variables, you aren't defining clock_rate and read_size in all possible cases. Do something like the following:

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

Instead of raising an error in each case, you could provide default values (other than the previously assigned None ) to each variable.

I am going to put this as an answer instead of a comment to stop any confusion from happening. To use global variables, you do NOT have to have initialized them in the global scope. The following code will work perfectly:

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

some_function()
print(some_global)

Globals do not have to be initialized in the outer scope. Instead, they can be defined in any scope at any time. The only constraint is: they must be defined before accessed . This is what's wrong with your code.

In the 'downlink' mode, your function does not define read_size and in the 'uplink' mode, you are comparing the input against 0 and 1 , which are integers and therefore those expressions will never result in True . This means, the else block is reached that tells the user the input is invalid. In this case, neither read_size nor clock_rate will be defined before the final print statement.

You will need to declare the clock rate and read_size variables before you modify their scope with the global keyword so you can change them.

You also need an elif CE_enabled==0 otherwise you always get invalid input if you input 1.

See below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM