3

I have a data file containing steps on each line representing each day out of the year and I want to add the appropriate amount of lines to each month.

 #Initilize lists
listOfSteps = []
Jan = []
Feb = []
Mar = []
Apr = []
May = []
Jun = []
Jul = []
Aug = []
Sept = []
Oct = []
Nov = []
Dec = []

# Open file into program and insert data into list called listOfSteps
step_file = open('/Users/gregoryyelverton/Desktop/Data files/steps.txt', 'r')

for line in step_file:
        line = line.rstrip('\n')
        listOfSteps.append(line)



# Create list with all months inside
months = [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]

# Iterate through list containing months and insert
# the days within those months

for month in months:
    #print(month)
    if month == [Jan] or [Mar] or [May] or [Jul] or [Aug] or [Oct] or [Dec]:
        month.append(listOfSteps[:31])
        del listOfSteps[:31]
        print('31 days')

    if month == [Apr] or [Jun] or [Sept] or [Nov]:
        month.append(listOfSteps[:30])
        del listOfSteps[:30]
        print('30 days')

    else:    
        month.append(listOfSteps[:28])
        del listOfSteps[:28]
        print('28 days')

The problem I am running into is that each month isn't sent through the appropriate if loop. So they alternate between having 31/30 days and never even checks if it is supposed to have 28 days.

deshun_g
  • 33
  • 3
  • That is because your if loops are only checking the first month in the statement, the rest are all checking if the other months are truthy so it will always execute both the first conditional statements as long as those lists aren't empty. – Alexander May 04 '22 at 23:41
  • 1
    Does this answer your question? [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – mkrieger1 May 05 '22 at 00:31
  • @mkrieger1 that only addresses part of the problem here – Alexander May 05 '22 at 00:40

1 Answers1

2

When your code does if this == that or that1 or that2: it does a comparison of the this and that just like you expect it to, however it does not do the same comparison on the rest of the variables. Instead it's the same as if that1: which is just a truthy check.

So for example:

>>> a = 1
>>> b = 2
>>> c = 3
>>> if a == b or c:
...     print("This code runs even though a does not equal b or c")
...
This code runs even though a does not equal b or c
>>> if a == b or a == c:
...     print("this will not print because a does not equal b or c")
...
>>> 

The correct way to compare them would look like this:

if month == Jan or month == Mar or month == May or month == Jul or month == Aug or month == Oct or month == Dec:
    month.append(listOfSteps[:31])
    del listOfSteps[:31]
    print('31 days')
  • a better solution would probably be something like this:
if month in [Jan, Mar, May, ...]:
    month.append(...)
    ...

addressing your comment: When you compare lists in python it compares them elementwise, which is a problem when comparing two different lists with the same contents if you want them to be treated as different.

for example:

>>> a = [1]
>>> b = [1]
>>> c = a
>>> if c == b:
...    print("The contents are the same so this will print")
The contents are the same so this will print

A solution to this problem would be to perform an identity comparison using the id() function, instead of comparing them directly with each other.

so something like this:

if id(month) in [id(i) for i in [Jan, Mar, Apr ...]]:
    month.append(...)
    ...

So with all that in mind try this:

#Initilize lists
listOfSteps = []
Jan = []
Feb = []
Mar = []
Apr = []
May = []
Jun = []
Jul = []
Aug = []
Sept = []
Oct = []
Nov = []
Dec = []

# Open file into program and insert data into list called listOfSteps
step_file = open('/Users/gregoryyelverton/Desktop/Data files/steps.txt', 'r')

for line in step_file:
        line = line.rstrip('\n')
        listOfSteps.append(line)

# Create list with all months inside
months = [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]

# Iterate through list containing months and insert the days within those months

for month in months:
    #print(month)
    if id(month) in [id(i) for i in [Jan,Mar,May,Jul,Aug,Oct,Dec]]:
        month.append(listOfSteps[:31])
        del listOfSteps[:31]
        print('31 days')

    elif id(month) in [id(i) for i in [Apr,Jun,Sept,Nov]]:
        month.append(listOfSteps[:30])
        del listOfSteps[:30]
        print('30 days')

    else:    
        month.append(listOfSteps[:28])
        del listOfSteps[:28]
        print('28 days')
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • I implemented the changes and it does a better job at picking the right if statements but some of the months are satisfying more than one if statement causing me to run out of lines by the time I hit [Oct]. I am not sure why this is happening – deshun_g May 05 '22 at 00:02
  • 2
    You should turn the second `if` into an `elif`. Otherwise, a 31 day month will also enter the `else` – 2pichar May 05 '22 at 00:05
  • 1
    @deshun_g that is probably because when it does the comparison al it's doing is comparing the contents of the list. so if `Jan = [1]` and `Feb = [1]` and `month = Jan` then `month == Feb` is still true because the contents are the same. – Alexander May 05 '22 at 00:08
  • 1
    @2pichar good call – Alexander May 05 '22 at 00:09
  • 1
    @deshun_g I edited my answer. See if that solves the problem. – Alexander May 05 '22 at 00:30
  • 1
    @deshun_g I think this is the #1 mistake made by newbies to Python. At the very least it's in the top 10. – Mark Ransom May 05 '22 at 02:18