Python Forum

Full Version: Ignoring a list item
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all!
I need a bit of help ignoring a list item, I have a list of names with a "Y" at then end of each name and I want to ignore names without a "Y", my code is below:

from time import strftime # import time
print("Report date: " + strftime("%d/%m/%Y")) #Prints clock time

with open("confPack.txt", "r") as confPack: # open file as confpack
    cPack = confPack.read().splitlines() # create cpak variable

with open("employees.txt") as fp:
    for line in fp:
        values = line.strip().split(",")
        surname = values[0]
        firstName = values[1]

        # is the last value not a Y (meaning no Y's)

        # if that's false, meaning there is a Y, is the one before it a Y too
        if values[-1] != 'Y':
            print(" did not attend ")
        elif values[-2] == 'Y':
            packs = print(cPack[1])
        else:
            packs = print(cPack[0])
        print(f"Attendee: {surname}, {firstName}: {packs}")
Thanks in advance!
Lines 19 and 21 assign the value None to packs, so line 22 is either going to print "None" for {packs} or it is going to throw a name error because packs is not defined. Do you want to do something like this?
        if values[-1] != 'Y':
            packs = None
        elif values[-2] == 'Y':
            packs = cPack[1]
        else:
            packs = cPack[0]

        if packs is not None:
            print(f"Attendee: {surname}, {firstName}: {packs}")
I don't understand what the objective is. Nevertheless there is built-in str.endswith which could be used:

>>> s = 'happy'
>>> s.endswith('y')
True
>>> s.endswith('p', 0, -1)
True