【发布时间】:2021-02-01 15:40:48
【问题描述】:
调用函数后,我无法打印最终语句。我从这里尝试了其他解决方案,但似乎无法让 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_rate和read_size。为什么不让你的函数返回read_size和clock_rate呢? -
我尝试在函数外部定义变量,但仍然无法让 read_size 工作。我可以这样做,但这需要我重写更多的函数。
-
我强烈建议重写更多函数
-
函数的执行可能会也可能不会为这两个变量赋值——所以如果你要在调用后引用它们的值,你需要确保它们存在并且在调用它之前有一些默认值它。