-1

我是 python 和一般编码的初学者,不知道为什么下面的代码中的 return 不会跨函数移动。不应该存储 n 的值以供所有函数使用吗?print(n) 在那里只是为了我自己看它是否有效,显然不是。

def main():
    print("This program tests the Goldbach's conjecture")
    get_input()
    print(n)


def get_input():
    n = 0
    while n == 0:
        try:
            v = int(input("Please enter an even integer larger than 2: "))
            if v <= 2:
                print("Wrong input!")
                continue
            if v%2 == 1:
                print("Wrong input!")
                continue
            if v%2 == 0:
                n = v
        except ValueError:
            print("Bad input!")
            continue
    return n
4

1 回答 1

1

您没有存储get_input任何地方返回的值,您应该将其保存在变量中(或直接打印),例如 -

def main():
    print("This program tests the Goldbach's conjecture")
    val = get_input()
    print(val)

n是一个内部变量,仅存储在get_input.

于 2021-03-26T22:49:05.313 回答