Home > Back-end >  Trying to pull out names of lists which have a low value. Can't get logic to work
Trying to pull out names of lists which have a low value. Can't get logic to work

Time:12-13

Trying to print out which days have less than 7 hours. I've tried several different ways of making it work to no avail.

day_one_list = ['Day 1', 5]
day_two_list = ['Day 2', 8]
day_three_list = ['Day 3', 6]
day_four_list = ['Day 4', 8]
day_five_list = ['Day 5',2]
    
    
 def slack_days(day_one_list, day_two_list, day_three_list, day_four_list, day_five_list):
        days_list = [day_one_list, day_two_list, day_three_list, day_four_list, day_five_list] 
            for name, hours in days_list:
                if hours < 7:
                    print(f"On {name} you worked {hours}.")

slack_days(day_one_list, day_two_list, day_three_list, day_four_list, day_five_list)

CodePudding user response:

for name, hours in days_list doesn't unpack the nested lists.

Try the following

def slack_days(*args):
    for day in args:
        if day[1] < 7:
            print(f"On {day[0]} you worked {day[1]}.")

slack_days(day_one_list, day_two_list, day_three_list, day_four_list, day_five_list)

Or just use a regular, flat list

day_hours = [5,8,6,8,2]
for day, hour in enumerate(day_hours):
    if hour < 7:
        print(f"On day {day 1} you worked {hour}.")

CodePudding user response:

It looks like you could use a dict :)

def slack_days(days): return [
    f'On {day} you worked {hours} hours' 
    for day,hours in days.items()
    if hours < 7
]

days = {
    'Day 1': 5,
    'Day 2': 8,
    'Day 3': 6,
    'Day 4': 8,
    'Day 5': 2,
}

for output in slack_days(days);
    print(output)
  • Related