【发布时间】:2021-03-02 14:37:57
【问题描述】:
我有一个 .txt 文件 (update.txt),其中记录了我对程序所做的更新更改。内容如下:
make Apples 4
make Oranges 3
outstanding Bananas 1
restock Strawberries 40
restock Pineapples 23
如何根据我选择的选项适当地打印出来(例如,我只想打印出所有“补货”数据)。输出应该是这样的:
0-restock, 1-make, 2-outstanding. Enter choice: 0
Summarised Data for RESTOCK ***
Strawberries 40
Pineapples 23
下面的代码是正确的方法吗?
choice = int(input("0-restock, 1-make, 2-outstanding. Enter choice: "))
if choice == 0:
print("Summarised Data for RESTOCK ***")
with open("update.txt") as openfile:
for line in openfile:
for part in line.split():
if "restock" in part:
print(f"{fruitType} {quantity}")
【问题讨论】:
-
不行,代码有明显的缩进错误。
标签: python python-3.x file-handling