<tfoot id='ze4uw'></tfoot>

      <small id='ze4uw'></small><noframes id='ze4uw'>

      <legend id='ze4uw'><style id='ze4uw'><dir id='ze4uw'><q id='ze4uw'></q></dir></style></legend>
        <i id='ze4uw'><tr id='ze4uw'><dt id='ze4uw'><q id='ze4uw'><span id='ze4uw'><b id='ze4uw'><form id='ze4uw'><ins id='ze4uw'></ins><ul id='ze4uw'></ul><sub id='ze4uw'></sub></form><legend id='ze4uw'></legend><bdo id='ze4uw'><pre id='ze4uw'><center id='ze4uw'></center></pre></bdo></b><th id='ze4uw'></th></span></q></dt></tr></i><div id='ze4uw'><tfoot id='ze4uw'></tfoot><dl id='ze4uw'><fieldset id='ze4uw'></fieldset></dl></div>

        • <bdo id='ze4uw'></bdo><ul id='ze4uw'></ul>

      1. Python - 函数返回值

        Python - Function Return Value(Python - 函数返回值)

        <small id='z1cw2'></small><noframes id='z1cw2'>

            <tbody id='z1cw2'></tbody>

        • <i id='z1cw2'><tr id='z1cw2'><dt id='z1cw2'><q id='z1cw2'><span id='z1cw2'><b id='z1cw2'><form id='z1cw2'><ins id='z1cw2'></ins><ul id='z1cw2'></ul><sub id='z1cw2'></sub></form><legend id='z1cw2'></legend><bdo id='z1cw2'><pre id='z1cw2'><center id='z1cw2'></center></pre></bdo></b><th id='z1cw2'></th></span></q></dt></tr></i><div id='z1cw2'><tfoot id='z1cw2'></tfoot><dl id='z1cw2'><fieldset id='z1cw2'></fieldset></dl></div>
              <bdo id='z1cw2'></bdo><ul id='z1cw2'></ul>
              <tfoot id='z1cw2'></tfoot>

                  <legend id='z1cw2'><style id='z1cw2'><dir id='z1cw2'><q id='z1cw2'></q></dir></style></legend>
                  本文介绍了Python - 函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个例子只是一个基本的程序——我是一个新的程序员——一边学习和试验,一边搞砸..目前在 Python 3.6 IDE 和 PyCharm 上进行测试 - 为双倍行距代码道歉 - 但如果没有的话,看起来一团糟.

                  This example is just a basic program - I'm a new Coder - learning and experimenting whilst messing about .. Currently testing on Python 3.6 IDE and PyCharm - apologies for double spacing code - but looks a mess without.

                  寻找从函数返回值的指导.

                  Looking for guidance for returning a value from a function.

                  已经尝试了几十种不同的方法/搜索了论坛,但是这个外行可以理解的最接近的答案是我需要使用返回值,否则会被遗忘..所以添加了 print(age_verification," 示例测试值.. ") 在不同的位置 - 但在函数之外没有返回任何内容..

                  Have tried dozens of different methods / searched the forum, but closest answer this layman could understand stated I needed to use the return value otherwise it will be forgotten .. So added print(age_verification, " example test value .. ") at various locations - but nothing gets returned outside the function ..

                  已尝试返回布尔值/整数/字符串值并进行调整 - 每个变体都没有.在函数之前添加了一个默认的 age_verification = False 变量//或//第一次在函数中引用.. 不影响返回值,除非 IDE 没有声明未解析的引用"

                  Have tried returning Boolean / integer / string values and adapting - nothing with each variant .. Added a default age_verification = False variable before the function // or // referenced within function for 1st time .. Doesn't effect the return value except IDE doesn't state "unresolved reference"

                  尝试了逐行 python 可视化工具 - 但又一次 - age_verification 值在退出函数后立即消失.:-(

                  Tried a line-by-line python visualizer - but again - age_verification value disappears instantly after exiting the function . :-(

                  ====================================================================

                  ==================================================================

                  使用 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)
                  

                  ===============================================================

                  ==============================================================

                  任何建议都值得赞赏 - 今天大部分时间都浪费在我的头上 .. 并提前道歉.. 知道这将是非常基本的东西 - 但似乎使用与其他人相同的格式:-)

                  Any advice is appreciated - wasted most of today hitting my head against the wall .. And apologies in advance .. Know it'll be something really basic - but appear to be using same formatting as others :-)

                  谢谢

                  推荐答案

                  print 不返回值,它只会将值显示到 stdout 或控制台.如果你想返回一个带条件的值,理解范围是有帮助的.您关于返回变量的评论,否则它们将被遗忘"是正确的.函数定义的和返回的变量会在函数执行时消失:

                  print doesn't return values, it will just display the value to stdout or the console. If you want to return a value with conditions, understanding scope is helpful. Your comment regarding returning variables otherwise they will be "forgotten" is correct. Variables defined and not returned by the function will go away when the function executes:

                  def my_func(var1):
                      var2 = var1
                      var3 = 5
                      return var3
                  
                  print(my_func(1), var2)
                  

                  print 语句将抛出 NameError,因为 var2 没有在函数外部定义,也没有返回.对于您的示例,您需要这样的内容:

                  The print statement will throw a NameError because var2 isn't defined outside of the function, nor is it returned. For your example, you'd want something like this:

                  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

                  This way you are saving the returned value as the variable age_verification

                  为了进一步扩展作用域概念,对 my_func 使用相同的定义:

                  To further expand on the scope concept, using the same definition for my_func:

                  def my_func(var1):
                      var2 = var1
                      var3 = 5
                      return var3
                  

                  我们将返回的 var3 分配给一个变量,如下所示:

                  We assign the returned var3 to a variable like so:

                  myvar = my_func(5)
                  

                  如前所述,名称 var3 实际上并没有返回,只是返回值.如果我要跑

                  As noted, the name var3 isn't actually returned, just the value. If I were to run

                  myvar = my_func(5)
                  print(var3)
                  

                  我会得到一个 NameError.解决这个问题的方法是:

                  I would get a NameError. The way to get around that would be to do:

                  var3 = my_func(5)
                  

                  因为现在 var3 是在全局范围内定义的.否则,我将不得不编辑我的函数以使 var3 全局:

                  because now var3 is defined in global scope. Otherwise, I would have to edit my function to make var3 global:

                  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
                  

                  希望这比我原来的答案更清楚

                  Hopefully this is a bit clearer than my original answer

                  这篇关于Python - 函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <legend id='DIWEY'><style id='DIWEY'><dir id='DIWEY'><q id='DIWEY'></q></dir></style></legend>

                    <small id='DIWEY'></small><noframes id='DIWEY'>

                      <tbody id='DIWEY'></tbody>
                    • <tfoot id='DIWEY'></tfoot>

                      <i id='DIWEY'><tr id='DIWEY'><dt id='DIWEY'><q id='DIWEY'><span id='DIWEY'><b id='DIWEY'><form id='DIWEY'><ins id='DIWEY'></ins><ul id='DIWEY'></ul><sub id='DIWEY'></sub></form><legend id='DIWEY'></legend><bdo id='DIWEY'><pre id='DIWEY'><center id='DIWEY'></center></pre></bdo></b><th id='DIWEY'></th></span></q></dt></tr></i><div id='DIWEY'><tfoot id='DIWEY'></tfoot><dl id='DIWEY'><fieldset id='DIWEY'></fieldset></dl></div>

                          • <bdo id='DIWEY'></bdo><ul id='DIWEY'></ul>