我非常仓促地编写了一个程序,看看我是否可以改进我放弃的一些旧代码,因为我无法将其保存但是现在我已将它写入.csv文件它什么都不做。如果我在它运行时尝试与它进行交互,即按1,2,3,4然后没有任何反应,它甚至没有原因退出,所以我无法解决它。这是代码,谁能告诉我我缺少什么?
import pandas as pd
import csv
import matplotlib.pyplot as plt
import numpy as np
myDict = {"Apples": 150, "Berries": 200, "Bananas": 300, "Oranges": 350}
def menu():
print("Press 1: To add stock. ")
print("Press 2: To check stock. ")
print("press 3: To enter purchase. ")
print("Press 4: To quit the program. ")
return input("What would you like to do? ")
run = menu()
while True:
if run == '1':
AddStock = input('Product to be added to stock? ')
Amount = int(input('Quantity of product to be added to stock? '))
myDict[AddStock] = Amount
run = menu()
elif run == '2':
for key, value in myDict.items():
print("{}: {}".format(key, value))
run = menu()
elif run == '3':
product = input('What product was sold? ')
if product in myDict:
myDict[product] -= 1
else:
print("Product is out of stock! ")
run = menu()
elif run == '4':
with open('stock.csv', 'w') as stock:
writer = csv.writer(stock)
for key, value in myDict.items():
writer.writerow([key, value])
break
答案 0 :(得分:0)
既然没有人回答这个问题......这是解决问题的快捷方法。虽然,我只会显示一次命令选项,而不是每次都显示。
import csv
myDict = {"Apples": 150, "Berries": 200, "Bananas": 300, "Oranges": 350}
def menu():
print("Press 1: To add stock.")
print("Press 2: To check stock.")
print("press 3: To enter purchase.")
print("Press 4: To quit the program.")
return input("What would you like to do? ")
run = None
while run != '4':
run = menu()
if run == '1':
AddStock = input('Product to be added to stock? ')
Amount = int(input('Quantity of product to be added to stock? '))
myDict[AddStock] = Amount
elif run == '2':
for key, value in myDict.items():
print("{}: {}".format(key, value))
elif run == '3':
product = input('What product was sold? ')
if product in myDict:
myDict[product] -= 1
else:
print("Product is out of stock! ")
elif run == '4':
with open('stock.csv', 'w', newline="") as stock:
writer = csv.writer(stock)
for key, value in myDict.items():
writer.writerow([key, value])