【发布时间】:2015-11-21 01:00:53
【问题描述】:
我的代码不会写入文件,我做错了什么?请帮忙。我正在尝试编程以继续询问产品,直到用户不输入产品代码。我希望所有产品都保存在文件中。
store_file = open("Database.txt", "w")
NewProduct = ""
while NewProduct != False:
contine = input("Press 1 to enter a new product press 2 to leave: ")
if contine == "1":
print("Enter your product information")
information = []
product = input("What's the product code: ")
information.append(product)
description = input("Give a description of the product: ")
information.append(description)
price = input("Enter price of product: ")
information.append(price)
information = str(information)
clean = information.replace("]","").replace("[","").replace(",","").replace("'","")
store_file.write(clean)
elif contine == "2":
NewProduct = False
else:
print("Your input is invalid")
store_file.close
【问题讨论】:
-
这是 Python 2 还是 Python 3?有很大的不同;在 Py2 上,此代码保证说所有输入都是无效的。不过,我们需要知道实际错误(或缺少错误)才能确定。
-
我正在使用 python 3。没有错误出现,但问题是创建了 database.txt 文件但不包含任何数据。
标签: python file iteration writing