-3

我正在尝试编写一个程序,该程序向每个收件人和每个地址写入相同的字母,但每个字母将具有不同的名称和地址。

这就是我到目前为止所拥有的。

def main():
    recipients = [] #should be names
    adresses = [] #letters and numbers

    filename = input("Please enter the name of the template file: ")
    file = open(filename, 'w')
    recipients = input("Please enter names of recipients: ")
    adresses = input("Please list addresses: ")

    for names in recipients:
        letter = "New York, New York\n17 November 2013\n \n", recipients[names], "\n" ,adresses, "\n\nDear",recipients[names],"\n\n" \
        "Thank you for your service to New York City, and\nin particular, to the education of its residents.\nThose in", adresses, "appreciate it!"\
        "\n\nBest wishes to", recipients[names], "and your family,\n\n"

        file.write(str(letter))

    file.close()
main()

当它写出文件时,它只写一个字母并将所有内容放在一起。

我想要做的是写一封信给不同的人和同一个文件中的不同地址。

前任。这都将在同一个文件中。

   New York, New York
   11 March 2013

   person1
   Bronx NY

   Dear person1,

   Thank you for your service to New York City, and,
   in particular, to the education of its residents.
   Those in Bronx NY appreciate it!

   Best wishes to person1 and your family   


   New York, New York
   11 March 2013

   Person2
   New York NY

   Dear person2,

   Thank you for your service to New York City, and,
   in particular, to the education of its residents.
   Those in New York NY appreciate it!

   Best wishes to person2 and your family

有什么帮助吗?

使用 Python 3.x

提前致谢

4

3 回答 3

2

您的代码有几个问题。

首先,您请求的是姓名和地址列表,但您只能从input. 如果您希望您的用户指定多个值,则需要以某种方式解析该输入字符串以拆分这些值。例如,您可以用逗号分隔:

recipients_str = input("Please enter names of recipients: ")
recipients = recipients_str.split(",")

您遇到的下一个问题是您的循环没有按照您的预期执行。在 Python 中,for循环为您提供循环序列中的值,而不是索引。所以,试试:

for name in recipients:

或者,由于您还有一个应该对应于名称列表的地址列表,因此请zip同时使用并获取名称和地址:

for name, address in zip(recipients, addresses):

接下来,您对创建字母字符串的代码有疑问。您当前正在制作一个字符串元组,而不是单个字符串。更好的方法是用于str.format处理将各种值插入到模板中:

letter_template = """New York, New York
11 March 2013
{name}
{address}

Dear {name},

Thank you for your service to New York City, and,
in particular, to the education of its residents.
Those in {address} appreciate it!

Best wishes to {name} and your family
"""

letter = letter_template.format(name=name, address=address)
于 2013-11-12T01:25:08.680 回答
0

您需要为每个收件人写入一个单独的文件,而不是像您当前所做的那样写入同一个文件:

def main():
    recipients = [] #should be names
    adresses = [] #letters and numbers

    recipients = input("Please enter names of recipients: ")
    adresses = input("Please list addresses: ")

    for names in recipients:
        letter = "CONTENTS OF LETTER"

        filename = input("Please enter the name of the template file: ")
        file = open(filename, 'w')
        file.write(str(letter))
        file.close()

main()

请注意,每次循环迭代都会提示您输入新的模板文件名。您还需要确保每个模板名称都是唯一的,这样之前的文件就不会被覆盖。

于 2013-11-12T01:12:35.897 回答
0

你的问题是你需要为每个字母写一个唯一的文件,下面的代码部分应该被修改为每个字母创建一个文件,你可以做一些事情,比如创建一个计数器并将其附加到文件名以保持它们的唯一性。

    for names in recipients:
        letter = "New York, New York\n17 November 2013\n \n", recipients[names], "\n" ,adresses, "\n\nDear",recipients[names],"\n\n" \
        "Thank you for your service to New York City, and\nin particular, to the education of its residents.\nThose in", adresses, "appreciate it!"\
        "\n\nBest wishes to", recipients[names], "and your family,\n\n"

        file.write(str(letter))

    file.close()
于 2013-11-12T01:13:54.100 回答