简体   繁体   English

在 Python 中,是否可以将 if-then 语句分配给变量?

[英]In Python, is it possible to assign an if-then statement to a variable?

I've defined a few variables below and I want to use them to define a variable.我在下面定义了一些变量,我想用它们来定义一个变量。

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']

I then want to use these to define the fiscal year I'm using.然后我想用这些来定义我正在使用的财政年度。 I tried both methods below (the first one was what I was really looking for), but both just said "invalid syntax".我尝试了以下两种方法(第一种是我真正想要的),但都只是说“无效语法”。

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)

Finally, I want to use that "test_year" variable in other places in my code.最后,我想在我的代码的其他地方使用那个“test_year”变量。 Anyway, any suggestions on how to make this work?无论如何,关于如何使这项工作的任何建议?

The first piece of code is invalid because you're trying to assign a value while doing boolean.第一段代码无效,因为您在执行 boolean 时尝试分配值。 For the second, you forgot the () that would go after test_year to define the paramters.对于第二个,您忘记了在 test_year 之后将 go 定义参数的 ()。 It should be def test_year(curr_month): .它应该是def test_year(curr_month): To use the function in your code, call it using test_year(current_month_variable) .要在代码中使用 function,请使用test_year(current_month_variable)调用它。

A function needs to at least have parentheses, even if it takes no parameters, so def test_year: won't work.一个 function 至少需要有括号,即使它不带参数,所以def test_year:不会工作。 An if statement can't be assigned to a variable. if语句不能分配给变量。 Instead, you could define a function that accepts curr_month and list_early_fy as parameters,相反,您可以定义一个接受curr_monthlist_early_fy作为参数的 function,

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

And you'd call it via test_year(curr_month, curr_year, list_early_fy) .你可以通过test_year(curr_month, curr_year, list_early_fy)来调用它。

If you had a class that stored curr_month , curr_year , and list_early_fy , you could reference those class variables:如果您有一个存储curr_monthcurr_yearlist_early_fy ,则可以引用这些 class 变量:

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()

First, you are not defining test_year, you are just printing cuur_year.首先,您没有定义 test_year,您只是在打印 cuur_year。 Then: Your second code will work if you add parenthese, like this:然后:如果您添加括号,您的第二个代码将起作用,如下所示:

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

And one line if else based variable assignment (i guess this is what you wanted initially) works like this:一行 if else 基于变量赋值(我想这是你最初想要的)的工作方式如下:

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

Above in normal syntax:上面的正常语法:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM