【问题标题】:Function with condition does not return results [closed]有条件的函数不返回结果[关闭]
【发布时间】:2021-12-30 22:50:58
【问题描述】:

为什么我不能打印这个函数的条件结果?为什么我不能返回这个函数的各种结果?我什至无法打印它们。我究竟做错了什么?如何解决?

我没有收到错误,但没有打印任何内容,就好像该功能不可见一样。我也尝试用 island() 回调它,但它不打印

def island():

    select_robot_on_her_island = select_war.split('-')[0]      
    
    cursor.execute("""
        SELECT
            CASE WHEN robot_on_her_island = ? THEN 'Last_War_on_her_island'
                 ELSE 'Last_War_on_island_away'
            END AS which_island,
            points_war_home, points_war_away
        FROM war
        WHERE ? IN (robot_on_her_island, robot_on_island_away)
        LIMIT 1""",
        (select_robot_on_her_island, select_robot_on_her_island))
    row = cursor.fetchone()

    if row:
        which_island, points_home, points_away = row
        if which_island == 'Last_War_on_her_island':
            return points_home_left = points_home
            return points_away_left = points_away
            return print("aaaa", which_island, points_home, points_away)
        else:
            # Swap the points when won on the away island
            return points_away_right = points_away
            return points_home_right = points_home
            return print("bbbb", which_island, points_away, points_home)

【问题讨论】:

  • 只要你点击了return 语句,函数就会结束,其他return 语句将不会执行。你想在这里做什么?您的 return 语句是分配。你希望函数返回什么?
  • 这不能是您的代码。对于那些带有赋值的返回语句,这会出现语法错误。请向我们展示您实际使用的代码。
  • @TimRoberts 根据条件是假还是真,我想返回两件事:points_home_left = points_home 和 points_away_left = points_away,然后我想打印它们只是为了测试。所以我想为 True 获得 2 件东西或为 False 获得 2 件东西
  • @Bobby - 您的代码不是有效的 Python 语法,并且与您引用的代码不同。
  • 你为什么不能做points home_left = points_home; points_away_left = points_away; return points_home_left, points_away_left 作为三个单独的行?虽然我不确定我为什么需要分配新变量而不是只返回 points_homepoints_away,但您更接近代码。

标签: python python-3.x function return


【解决方案1】:

我想这就是你所追求的。你可以从一个函数中返回两件事,但这必须在一个语句上完成。

def island():

    select_robot_on_her_island = select_war.split('-')[0]      
    
    cursor.execute("""
        SELECT
            CASE WHEN robot_on_her_island = ? THEN 'Last_War_on_her_island'
                 ELSE 'Last_War_on_island_away'
            END AS which_island,
            points_war_home, points_war_away
        FROM war
        WHERE ? IN (robot_on_her_island, robot_on_island_away)
        LIMIT 1""",
        (select_robot_on_her_island, select_robot_on_her_island))
    row = cursor.fetchone()

    if row:
        which_island, points_home, points_away = row
        print("aaaa", which_island, points_home, points_away)
        if which_island == 'Last_War_on_her_island':
            return points_home, points_away
        else:
            return points_away, points_home
    else:
        return None

【讨论】:

  • 我用了points_home_left = points_home而不是points_home,因为points_home_left我想以后或者将来因为其他原因使用,所以我想在函数内部创建它。有没有办法让points_home_left = points_home返回?
  • 没有。现在,调用island() 的人当然可以将返回值存储在任何它想要的位置。可以在函数中设置全局变量,但这几乎总是一个坏主意。只需让函数返回“事实”,然后让调用者决定如何处理它们。也许您需要返回 home/away 状态,如 return "home", points_home, points_awayreturn "away", points_home, points_away
  • 您可以使用 Tim 发布的代码,并在调用该函数时使用 home_left, home_right = island() 或您可能想要的任何其他名称。 Return 不将变量名称传递给校准器,仅从函数返回变量的值。
【解决方案2】:
  1. 第一个被执行的 return 中断了函数,这意味着它后面的任何 return 语句都不会被执行。但是你可以使用一个元组从一个函数中传递多个值:

    def my_func():
        return "one", "two"
    a, b = my_func()
    
  2. 打印显示东西,它不返回任何东西,所以不要使用return print()

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2014-12-29
    • 1970-01-01
    • 2016-11-11
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多