-3

I have too much input for an If-statement to place it all on one line, is there a way to transfer it onto the next line ?

if monthConversion != "Jan" or "Feb" or "Mar" or "Apr" or "May" \
                      or "Jun" or "Jul" or "Aug" or "Sep" or "Oct" or "Nov" or "Dec":

I tried to write this all on one line but it didn't fit. I then tried to put "or" after "Oct" and write "Nov" on the next following line, but Python says "Expression expected" and "Illegal variable for target annotation".

Nick
  • 123,192
  • 20
  • 49
  • 81
  • I would put the Months in a List initialized at the top of the module and then use "contains" so I wouldn't need a second line. – Frank Merrow Feb 06 '20 at 06:29
  • 1
    You have a bigger problem: your statement is always `True`. You are testing ìf monthConversion != "Jan" and then you test if "Feb" is truthy ... which it is - because emtpy strings are True – Patrick Artner Feb 06 '20 at 06:29
  • The "How to test multiple .." dupe tests one thing against multiple others, its the same if you switch your tests around: `"Jan" != mothConverstion and "Feb" != mothConverstion and ...` - but for you its probably better to do `monthConversion not in {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Now","Dec"}` or use datetime module or calendar module to do your conversion to start with – Patrick Artner Feb 06 '20 at 06:38
  • @patrick I ment to say: :.. non empty strings are `True` ... – Patrick Artner Feb 06 '20 at 06:40
  • The answer to the _original_ question: if you want to extend your expression to the next line, put a backslash at the end of the current line. – DYZ Feb 06 '20 at 06:48

5 Answers5

1
if monthConversion not in  {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}:
Olvin Roght
  • 6,675
  • 2
  • 14
  • 32
1

You can simplify/shorten the code by putting the month names into a list and checking if monthConversion is in the list:

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
monthConversion = "Xyz"
if monthConversion not in months:
    print("Invalid month " + monthConversion)
Nick
  • 123,192
  • 20
  • 49
  • 81
1

Although there isn't really a limit to how long a line can be, I'm assuming your editor puts some restriction on maximum line length.

Also, if x != 'a' or 'b' doesn't do what you think it does. 'b' by itself is not '' and will evaluate to True, so the whole expression is always True, instead of checking if x is either 'a' or 'b'.

The better way here is to just break it into a few, more readable statements, like:

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
if monthConversion not in months:
    print('something')

Also: don't use monthConversion, but month_conversion instead, if you can. Learning Python naming conventions early is a good idea.

Why aren't you using existing date functions in Python though?

import calendar

if month_conversion in calendar.month_abbr:
    print('something')
Grismar
  • 20,449
  • 4
  • 26
  • 46
1

You have been given many clever suggestions. Here is another one: do not reinvent the wheel, use a library function.

import calendar
if monthConversion not in calendar.month_abbr:
    do_something()

But bear in mind: this solution is 1000 times slower than the solution proposed by Olvin Roght. If performance is important, go for that one.

DYZ
  • 51,549
  • 10
  • 60
  • 87
-1

You can use:

if monthConversion not in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]:

Vaibhav Jadhav
  • 2,000
  • 1
  • 5
  • 19