简体   繁体   中英

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. 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. For the second, you forgot the () that would go after test_year to define the paramters. It should be def test_year(curr_month): . To use the function in your code, call it using 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. An if statement can't be assigned to a variable. Instead, you could define a function that accepts curr_month and list_early_fy as parameters,

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

If you had a class that stored curr_month , curr_year , and list_early_fy , you could reference those class variables:

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. 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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