Mubin_Hardy
Mubin_Hardy

Reputation: 63

why list of tuple is not callable? when i call a function with parameter as list of tuples it shows error

After defining a function for calling the above one(guest_list). How i print the elements if tuple(not list) in same line.for example

guest_list([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")])

i need out put asken is 30 years old and he is a chef

Upvotes: 0

Views: 591

Answers (5)

r1c4rdco5t4
r1c4rdco5t4

Reputation: 21

gguest_list = ([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")])

def guest_description(guest):
    for guest in guest_list:
        print(f'{guest[0]} is {guest[1]} years old and he is a {guest[2]}.')

guest_description(guest_list)

Upvotes: 0

mabergerx
mabergerx

Reputation: 1213

Given input list of tuples like:

[('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")]

You can achieve your desired output by using string formatting on each element:

# If you use Python 3.6+

item = ('Ken', 30, "Chef")

output = f"{item[0]} is {item[1]} years old and he is a {item[2]}"

# Older Python

output = "{} is {} years old and he is a {}".format(item[0], item[1], item[2])

Then again you might need to write some custom code that decides on the personal pronoun he/she in this case, but that is beyond the scope of your question.

Upvotes: 0

pjk
pjk

Reputation: 556

Try this:

def guest_list(guests):
    for guest in guests:
        print(f'{guest[0]} is {guest[1]} years old and he is {guest[2]}')

Upvotes: 0

MichaelJanz
MichaelJanz

Reputation: 1815

The following code should do as you want:

for guest in guest_list:
    name = guest[0]
    age = guest[1]
    job = guest[2]
    print(f"{name} is {age} years old and he is a {job}")

Upvotes: 0

kuro
kuro

Reputation: 3226

If I understand the problem correctly, you need to write guest_lists() function to print info of every guest. In that case, you can iterate over the list and print the info for each entry like -

def guest_list(guests):
    for (name, age, profession) in guests:
        print("{} is {} years old and is a {}".format(name, age, profession))

Upvotes: 3

Related Questions