我是 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