0

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?

David
  • 7
  • 4
  • 1
    What should be the `test year` equal to? Can you point with an example – Mohil Patel Jan 22 '21 at 18:42
  • 2
    It's not clear what value you want assigned to `test_year`. If you want a *function* named `test_year`, you are just missing the parentheses: `def test_year(): ...`. – chepner Jan 22 '21 at 18:43
  • Answer is No for your question. Refer this : [https://stackoverflow.com/questions/7872838/one-line-if-condition-assignment/47724773](https://stackoverflow.com/questions/7872838/one-line-if-condition-assignment/47724773) – Sridhar Dhandapani Jan 22 '21 at 18:46

3 Answers3

0

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

lwashington27
  • 320
  • 2
  • 14
0

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()
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • Sorry if this is obvious; I'm still relatively new to Python. I ran the first section of your solution and then tried to do: `print(test_year(curr_month, curr_year, list_early_fy))` This just gave a result of "None". Is that expected? – David Jan 22 '21 at 20:32
  • @David yes, because `test_year` doesn't return anything. You could replace `print(int(curr_year)+1, curr_year)` with `return int(curr_year)+1, curr_year` in order for it to print the result via `print(test_year(curr_month, curr_year, list_early_fy))`. – Random Davis Jan 22 '21 at 20:53
  • I was still getting `None` when I replaced that piece, but that was really just important for testing. So if I keep the `return` code in there, and use something like `this_yr = test_year(curr_month, curr_year, list_early_fy)`, then for instance, assigning a file name to be like `"FY" + str(this_yr)` should give FY21, correct? – David Jan 22 '21 at 22:13
  • @David no, because you're returning a tuple of `(int(curr_year)+1, curr_year)`, and a tuple's string representation is like `(item1, item2, ...)`. So the output will be `"FY(22, 21)"`. If you returned `curr_year`, then the output would be `"FY21"`. Passing multiple arguments to `print` works differently - `print(22, 21)` would print out `22 21`. – Random Davis Jan 22 '21 at 22:22
  • an if else _can_ be assigned to a variable, example : `if_func = lambda param: return param / 2 if param > 5 else return param * 2`, usage : `if_func(4)` gives 8, `if_func(6)` gives 3. – TheEagle Jan 24 '21 at 17:48
0

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)
TheEagle
  • 5,808
  • 3
  • 11
  • 39