0

I want the location to appear when I ask for results. Why can't I display the employee of the month's location?

Code:

work_hours = [("Jao", "Ville", 800), ("Billy", "Jackson St", 400), ("Kevin", "Westside", 500)] def employee_check(work_hours): current_max = 0 employee_of_month = "" for employee, location, hours in work_hours: if hours > current_max: current_max = hours employee_of_month = employee else: pass # return the statement return (employee_of_month, location, current_max) result = employee_check(work_hours) print(result) 
2
  • 4
    Please don't post pictures of your code - post your actual code! Commented May 21, 2021 at 7:28
  • Unrelated to the question, but looking at the picture, it is recommended to use a monospace font for programming Commented May 21, 2021 at 7:31

3 Answers 3

2
current_max = 0 employee_of_month = '' employee_of_month_location = '' for employee,location,hours in work_hours: if hours > current_max: current_max = hours employee_of_month = employee employee_of_month_location = location else: pass return (employee_of_month, employee_of_month_location, current_max) 
Sign up to request clarification or add additional context in comments.

6 Comments

It's more common to save index of max if single item is complex.
Absolutely @OlvinRoght - this was just to illustrate to OP what their existing code pattern was missing
YAY! THE CODE WORKED! THANK YOU VERY MUCH MATE @gvee
@JaocquinP, but it's not necessary to reinvent the functions. You should learn built-it functions to easy your programming experience with Python
ah i see. Thanks bro!
|
2

In this case more convenient is using built-in function max, not reinventing its functionality:

max(work_hours, key=lambda x: x[2]) 

Since your list consists of items with the same structure and you want to get only the tuple where the third element (the working hours) is maximum, you can use the key argument of the max function. You can pass a function there, in this case an anonymous lambda function. It changes the behavior of the max function, i.e. changing where exactly to look at the maximum value (here: the third position of each element in work_hours).

This reduces your code a lot as you can see how to use it:

work_hours = [("Jao", "Ville", 800), ("Billy", "Jackson St", 400), ("Kevin", "Westside", 500)] result = max(work_hours, key=lambda x: x[2]) 

4 Comments

Instead of your function
This is a very good solution to OP's question (upvoted it) using minimal built-in functionality. But it would be even better if you elaborate a bit on that or link to the python docu. For a beginner it is not really helpful to understand how to use it and how exactly the problem is solved. Therefore it is generally discouraged to give code-only answers. There are exceptions, but here I think not.
Thank you, I edited. I sought also the source from CPython, but didn't manage. If you know, where it is located, add, please.
I don't think it is necessary to go down to C-level. I've edited your answer. Please see this as a suggestion and feel free to change or revert it.
1
def employee_check(work_hours): current_max = 0 employee_of_month = '' employee_of_month_location = '' for employee,location,hours in work_hours: if hours > current_max: current_max = hours employee_of_month = employee employee_of_month_location = location else: pass return employee_of_month, employee_of_month_location, current_max 

1 Comment

You don't need else block, just remove it. Also parentheses is redundant in your return statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.