创建一个接收元组列表的函数,其中每个元组都有两个元素

时间:2016-09-25 19:00:00

标签: pythonanywhere

第一个元素是客户名称(字符串),第二个元素是客户的月份总帐单(浮动)。该功能的目的是处理此列表,创建"开票电子邮件"对于使用以下模板的每个客户:

'Dear {},\n\nYou owe ACME Corp ${:.2f}.\n\nBest,\nACME Corp'

该函数应返回这些生成的电子邮件的列表。它应该命名为" generate_billing_emails"。输入应该被称为" customer_data"

test_data = [("Wile E Coyote", 345.67), ("Bugs Bunny", 25.49), ("Foghorn Leghorn", 68.00), ("Tweety", 5.99)]

billing_emails = generate_billing_email(test_data)

for billing_email in billing_emails:        
    print("{}".format( billing_email ))
    print("\n\n--------")

我的代码到目前为止:

def generate_billing_email(test_data):
    billing_emails = 'Dear {},\n\nYou owe ACME Corp ${:.2f}.\n\nBest,\nACME Corp'
    return billing_emails

1 个答案:

答案 0 :(得分:0)

要执行您尝试执行的操作,必须使用for循环遍历数据。而且,您可以通过传入参数来使用format函数。没有必要告诉Python你是否使用了float,string,integer等......

以下是我将如何重写您的功能:

def generate_billing_email(test_data):
    emails = []
    for tupe in test_data:
        emails.append("'Dear {0},\n\nYou owe ACME Corp ${1}.\n\nBest,\nACME Corp'".format(tupe[0], tupe[1]))
    return emails
相关问题