【问题标题】:In Python, is it possible to assign an if-then statement to a variable?在 Python 中,是否可以将 if-then 语句分配给变量?
【发布时间】:2021-01-22 18:39:54
【问题描述】:

我在下面定义了一些变量,我想用它们来定义一个变量。

today = datetime.today()
datem = str(datetime(today.year, today.month, 1))
curr_month = datem[5:7]
curr_year = datem[2:4]
list_early_fy = ['04', '05', '06', '07', '08', '09', '10', '11', '12']

然后我想使用这些来定义我正在使用的会计年度。我尝试了以下两种方法(第一种是我真正想要的),但都只是说“无效语法”。

test_year = if curr_month in list_early_fy:
            print(int(curr_year)+1, curr_year)
    
def test_year:
        if curr_month in list_early_fy:
            print(int(curr_year)+1, curr_year)

最后,我想在我的代码的其他地方使用那个“test_year”变量。无论如何,关于如何使这项工作的任何建议?

【问题讨论】:

标签: python


【解决方案1】:

一个函数至少需要有括号,即使它不带参数,所以def test_year: 不起作用。 if 语句不能分配给变量。相反,您可以定义一个接受curr_monthlist_early_fy 作为参数的函数,

def test_year(curr_month, curr_year, list_early_fy):
        if curr_month in list_early_fy:
            print(int(curr_year)+1, curr_year)

您可以通过test_year(curr_month, curr_year, list_early_fy) 调用它。

如果您有一个存储curr_monthcurr_yearlist_early_fy 的类,则可以引用这些类变量:

class MyClass(object):
    def __init__(self, curr_month, curr_year, list_early_fy):
        self.curr_month = curr_month
        self.curr_year = curr_year
        self.list_early_fy = list_early_fy
    
    def test_year():
        if self.curr_month in self.list_early_fy:
            print(int(self.curr_year)+1, self.curr_year)

c = MyClass(curr_month, curr_year, list_early_fy)
c.test_year()

【讨论】:

  • 很抱歉,如果这很明显;我对 Python 还是比较陌生。我运行了您解决方案的第一部分,然后尝试这样做:print(test_year(curr_month, curr_year, list_early_fy)) 这只是给出了“无”的结果。这是预期的吗?
  • @David 是的,因为test_year 不返回任何内容。您可以将print(int(curr_year)+1, curr_year) 替换为return int(curr_year)+1, curr_year,以便它通过print(test_year(curr_month, curr_year, list_early_fy)) 打印结果。
  • 当我更换那块时,我仍然收到None,但这对于测试真的很重要。因此,如果我将return 代码保留在那里,并使用类似this_yr = test_year(curr_month, curr_year, list_early_fy) 的东西,那么例如,将文件名分配为"FY" + str(this_yr) 应该给FY21,对吗?
  • @David 不,因为您返回的元组为(int(curr_year)+1, curr_year),而元组的字符串表示形式类似于(item1, item2, ...)。所以输出将是"FY(22, 21)"。如果您返回curr_year,则输出将为"FY21"。将多个参数传递给 print 的工作方式不同 - print(22, 21) 将打印出 22 21
  • an if else can 赋值给变量,例如:if_func = lambda param: return param / 2 if param > 5 else return param * 2,用法:if_func(4) 给出 8,if_func(6) 给出 3。
【解决方案2】:

第一段代码无效,因为您在执行布尔运算时尝试分配值。对于第二个,您忘记了在 test_year 之后定义参数的 ()。应该是def test_year(curr_month):。要在代码中使用该函数,请使用 test_year(current_month_variable) 调用它。

【讨论】:

    【解决方案3】:

    首先,您没有定义 test_year,您只是打印 cuur_year。 然后:如果您添加括号,您的第二个代码将起作用,如下所示:

    def test_year():
            if curr_month in list_early_fy:
                print(int(curr_year)+1, curr_year)
    

    基于 if else 的变量赋值(我猜这就是你最初想要的)的工作方式如下:

    num = int(input("enter number : "))
    num = num * 2 if num < 5 else num / 2
    print(num)
    

    上面的正常语法:

    num = int(input("enter number : "))
    if num < 5:
        num = num * 2 # could be shortened to num *= 2
    else:
        num = num / 2 # could be shortened to num /= 2
    print(num)
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 2016-02-08
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2017-01-07
      • 2019-09-06
      • 2022-09-24
      • 2018-07-31
      相关资源
      最近更新 更多