Newbie here.
I have the following code:
def names(*args):
for x in args:
return sorted(args)
print(names("john", "allison", "tony", "melissa"))
I am able to sort the names but I'm not able to use the method .upper() in order to make the name all caps.
CodePudding user response:
The args is essentially a tuple. You can either use map to convert to upper, or append to a new list.
First way:
def toUpper(s):
return s.upper()
def names(*args):
return sorted(list(map(toUpper, args)))
Second way:
def names(*args):
upperNames = []
for x in args:
upperNames.append(x.upper())
return sorted(upperNames)
Both will return lists, just use tuple(names(...))
to get in tuple form.
CodePudding user response:
You can do it with a list comprehension:
def names(*args):
return sorted([i.upper() for i in args])
print(names("john", "allison", "tony", "melissa"))
Output:
['ALLISON', 'JOHN', 'MELISSA', 'TONY']
Edit: without a list comprehension, you can do this:
def names(*args):
upper_lst = []
for i in args:
upper_lst.append(i.upper())
return sorted(upper_lst)