【发布时间】:2016-08-20 15:27:21
【问题描述】:
我正在使用 Python 3。
这是我正在编写的脚本。它要求输入姓名/生日,接受该输入,并将其附加到列表中。然后将该列表写入另一个文件。
我已经对此进行了研究,但找不到它不起作用的原因。
这是我的代码:
print("""Enter the name and birthday of the person like this:
Adam 1/29
""")
all_birthdays = [ "none so far" ]
while True:
birthday = input("> ").upper()
if birthday == "":
break
if birthday == "LIST":
print(all_birthdays)
if birthday not in all_birthdays:
all_birthdays.append(birthday)
else:
print("This name/birthday is already known")
birthday_list = open('test.txt','w')
for bday in all_birthdays
birthday_list.write("%s\n" %bday)
第二次编辑:我添加了代码(最底部的 for 循环和创建文件)。它有效,但我似乎什至无法在任何地方找到该文件。有什么帮助吗?我怎样才能找到它并打开它? 此代码位于:Writing a list to a file with Python
【问题讨论】:
-
您缺少对 () 的函数调用的括号。添加这些括号后,代码对我来说很好。
-
达特茅斯表示
upper函数调用。
标签: python file loops python-3.x user-input