【发布时间】:2021-03-26 22:44:06
【问题描述】:
我是 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
【问题讨论】:
-
标记的重复是多余的。但是,几乎所有有关 Python 函数的教程都提供了此范围信息。请从intro tour 重复on topic 和how to ask。 Stack Overflow 无意取代现有的文档和教程。
标签: python return goldbach-conjecture