Reputation: 54
I have defined a python class that has name and age attributes. I'm trying to print the oldest and longest name of objects in this class. the oldest works fine but to find the longest name I can't use one function with len() and max() since len() does not take more than one argument . I have to define the name length as an attribute first and then define the max. Copy of my code is below. I appreciate your help :)
class NewClass:
def __init__(self, name, age):
self.name = name
self.age = age
self.length = len(name)
def Oldest(*args):
return max(args)
# def Longets(*args): this doesn't work since len takes one argument only
# return max(len(args))
def Longest(*args):
return max(args)
person1 = NewClass('Cindy', 24)
person2 = NewClass('Amy', 28)
person3 = NewClass('fifififi', 27)
print(f'The oldest person is {Oldest(person1.age,person2.age,person3.age)} years old')
print(f'longest name has {Longest(person1.length,person2.length,person3.length)} character')
Upvotes: 0
Views: 128
Reputation: 658
You can use a list comprehension:
def Longets(*args):
return max([len(arg) for arg in args])
Upvotes: 1