处理python中的列表和消息

时间:2016-10-27 22:21:50

标签: python list python-3.x messages

这里非常新,但我邀请3个人共进晚餐,我必须向他们发送一份来自名单的消息,邀请他们共进晚餐..我在开始时我已经

dinnerGuest = ['Steve Jobs', 'Tupac Shakur', 'Kobe Bryant']
message = """You have been cordially invited to an epic dinner.
Please RSVP as soon as possible. Thank you""" + dinnerGuest[0].title + "."
print(message)

我收到错误

Please RSVP as soon as possible. Thank you""" + dinnerGuest[0].title + "."
TypeError: Can't convert 'builtin_function_or_method' object to str implicitly

任何人都知道为什么?在此先感谢,我知道这是一个新手问题。我会到达那里。

2 个答案:

答案 0 :(得分:1)

dinnerGuest = ['Steve Jobs', 'Tupac Shakur', 'Kobe Bryant']
message = """You have been cordially invited to an epic dinner.
Please RSVP as soon as possible. Thank you""" + dinnerGuest[0].title() + "."
print(message)

title()是一个函数 - 它需要一些东西并将其变成其他东西。

无论何时使用函数,总是将括号放在最后。

答案 1 :(得分:0)

由于您想邀请列表中的所有访客,因此您必须使用循环遍历整个列表:

dinnerGuest = ['Steve Jobs', 'Tupac Shakur', 'Kobe Bryant']

message = """You have been cordially invited to an epic dinner. 
              Please RSVP as soon as possible. Thank you """

for i in dinnerGuest:
    print(message + i.title() + ".")

同样正如@Andrew Owen和@Francisco Couzo所提到的,你需要使用 title()而不是 title ,因为它是一种方法,并且在它之后需要()

相关问题