这个示例只是一个基本程序-我是一个新的Coder-正在学习和尝试,却一团糟..当前正在Python 3.6 IDE和PyCharm上进行测试-道歉两次间距代码-但看起来一团糟。
寻找有关从函数返回值的指导。
尝试了数十种不同的方法/在论坛上进行了搜索,但是该外行可以理解的最接近答案表明我需要使用返回值,否则它将被忘记..因此添加了 print(age_verification,“示例测试值。 。“)在各个位置-但是在函数之外什么也不会返回。
尝试返回布尔值/整数/字符串值,并且不对每个变体进行任何调整。..在函数//或//第一次在函数内引用之前添加了默认 age_verification = False 变量。 。不会影响返回值,除非IDE不会声明“未解析的引用”
尝试了逐行的python可视化工具-但再次出现-age_verification值在退出函数后立即消失。 :-(
================================================ ==================
使用1个单一功能
def age_veri(age, age_verification) :
if age < 18 :
age_verification = False
print(age_verification, " is false .. Printed to test variable ..")
return age_verification
elif age >= 18:
age_verification = True
print(age_verification, " is True.. Printed to test variable ..")
return age_verification
return age_verification # ( -- have tested with/without this single-indent line & with/without previous double-indent return age_verification line.)
age=int(input("Enter Your Age : ")
age_verification = False # ( -- have tried with / without this default value)
age_veri(age, False)
if age_verification is False:
print("You failed Verification - Age is Below 18 .. ")
elif age_verification is True:
print("Enter Website - Over 18yrs")
else:
print(" Account not Verified .. ")
================================================ ==================
同一示例-使用2个功能
def age_variable(age):
if age < 18:
age_verification = False
print (age_verification, " printing here to use value and help test function..")
return age_verification
elif age >= 18:
age_verification = True
print (age verification, " printing here to use value and help test function..")
return age_verification
return age_verification (tried with and without this line - single indent - same level as if / elif)
def are_verified(age_verification):
if age_verification is False:
print("Age Verification Failed .. ")
elif age_verification is True:
print("Visit Website .. ")
else:
print("Verification Incomplete .. ")
age = int(input("Enter Your Age : ")
age_variable(age)
are_verified(age_verification)
================================================ ===============
任何建议都值得赞赏-今天大部分时间都浪费在了我的头上..以及事先的道歉..知道这将是非常基本的东西-但似乎使用的格式与其他格式相同:-)
谢谢
答案 0 :(得分:1)
print
不返回值,它仅将值显示到stdout
或控制台。如果要返回带有条件的值,那么了解范围会很有帮助。您对返回变量的评论正确,否则将被“遗忘”。函数执行时定义的变量和 not 返回的函数将消失:
def my_func(var1):
var2 = var1
var3 = 5
return var3
print(my_func(1), var2)
print
语句将抛出NameError
,因为var2
不在函数外部定义,也不返回。例如,您想要这样的东西:
def age_verify(age):
if age < 18:
print("Failed Verification")
return False
else:
print("Verification Complete")
return True
# Call and assign to var
age = int(input("Enter Your Age : ")
age_verification = age_verify(age)
通过这种方式,您将返回的值保存为变量age_verification
编辑:
要进一步扩展范围概念,请对my_func
使用相同的定义:
def my_func(var1):
var2 = var1
var3 = 5
return var3
我们将返回的var3
分配给一个变量,如下所示:
myvar = my_func(5)
如前所述,名称var3
实际上并未返回,仅返回了值。如果我要跑步
myvar = my_func(5)
print(var3)
我会得到一个NameError
。解决该问题的方法是:
var3 = my_func(5)
因为现在var3
是在全局范围内定义的。否则,我将不得不编辑函数以使var3
成为全局变量:
def my_func(var1):
global var3
var2 = var1
var3 = 5
# The return statement is then a bit redundant
my_func(5)
print(var3)
# prints 5 in global scope
希望这比我原来的答案要清楚