【发布时间】:2020-09-30 15:00:56
【问题描述】:
在玩 Python 时,我想出了一个简单的超市模拟器,我可以在其中对商品及其价格进行分类。
N=int(input("Number of items: "))
n1=0
name1=str(input("Product: "))
price1=float(input("Price: "))
price1="%.2f $" % (price1)
n=n1+1
item1=f"{n}.{name1}---{price1}"
table=[]
table.append(item1)
while n<N:
name=str(input("Product: "))
price=float(input("Price: "))
price="%.2f $" % (price)
n=n+1
item=f"{n}.{name}---{price}"
table.append(item)
for x in range(len(table)):
print (table[x])
对于输入
Number of items: 3
Product: Milk
Price: 5
Product: Water
Price: 1
Product: Apple
Price: 3.49
对于输出
1.Milk---5.00 $
2.Water---1.00 $
3.Apple---3.49 $
我想将打印输出导出为 txt。文件以在另一个项目中使用它。
【问题讨论】:
-
这能回答你的问题吗? Directing print output to a .txt file
标签: python python-3.x string