python - 计算总交付量
问题描述
为了让您解决我的问题,我认为您必须自己运行它。我的问题在这一行 print(Customer_Name, "has spent", Customers[Customer_Name]['TotalAmount'], "in total")
让你更好地理解。例如这里,它说Michael has spent 60.0 in total
什么时候应该Michael has spent 20.0 in total
import datetime
import uuid # GET A RANDOM ID FOR THE CUSTOMER
from csv import DictWriter
from datetime import date # GET CURRENT DATE AND TOMORROW DATE
import os
def openFile(): # STARTS HERE
try:
os.startfile('data_entered.csv')
except Exception as e:
print(str(e))
# OPEN FILE WHEN USER(EMPLOYEE) WANTS TO
def closeFile():
try:
os.system('TASKKILL /F /IM notepad.exe')
except Exception as e:
print(str(e)) # ENDS HERE
file = open('CustomerNames.txt', 'a')
file1 = open('Orders_Per_Users.txt', 'a')
file2 = open('data_entered.csv', 'a')
ORDERS_FROM_EMPLOYEE = 0
x = -1
length = 0
Total_Amount = 0.0
Customer_List = []
Address_List = []
My_List = []
today = datetime.date.today()
Today_Key = date.toordinal(date.today())
Today_Date = date.today()
Tomorrow_Date = datetime.date.today() + datetime.timedelta(days=1)
Customers = {}
Dates = {}
print("Welcome to our coffee shop!")
print("Login")
# EMPLOYEE LOGIN PROCESS STARTS
UserLogin = {"Employee1": "coffeeshop1", "Employee2": "coffeeshop2", "Employee3": "coffeeshop3"}
username = password = ''
while True:
username = input("Username: ")
password = input("Password: ")
if (username in UserLogin) and (UserLogin[username] == password):
print("Login Successful")
break
else:
print("Invalid Login. Try again")
# EMPLOYEE LOGIN PROCESS ENDS
# PROCESS AFTER ORDER PLACEMENT STARTS
print("Current Date is: {}".format(Today_Date))
process1 = True
process2 = True
while process1:
while process2:
x += 1
Customer_Name = input("Customer's Name:")
Customer_Address = input("Customer's Address:")
if Customer_Name in Customer_List and Customer_Address in Address_List:
First_Index = Customer_List.index(Customer_Name)
Second_Index = Address_List.index(Customer_Address)
if Customer_Name == Customer_List[First_Index]:
Customer_List.pop(First_Index)
x -= 1
Address_List.append(Customer_Address)
Customer_List.append(Customer_Name)
if Today_Key not in Dates:
Dates[Today_Key] = {}
if Customer_Name not in Dates[Today_Key]:
Dates[Today_Key][Customer_Name] = 1
else:
Dates[Today_Key][Customer_Name] += 1
if Customer_Name in Customers:
Customers[Customer_Name]['Orders'] += 1
Customers[Customer_Name]['TotalAmount'] = Total_Amount
else:
Customers[Customer_Name] = {}
Customers[Customer_Name]['Name'] = Customer_Name
Customers[Customer_Name]['Address'] = Customer_Address
Customers[Customer_Name]['ID'] = uuid.uuid1()
Customers[Customer_Name]['Orders'] = 1
Customers[Customer_Name]['TotalAmount'] = 0
Order_Price = float(input("Total amount of order:"))
ORDERS_FROM_EMPLOYEE += 1
print(Customer_Name, "has ordered {} time(s)".format(Customers[Customer_Name]['Orders']))
Total_Amount = Order_Price + Total_Amount
if Tomorrow_Date == Today_Date: # WHEN THIS IS TRUE, IT MEANS THAT THE DATE CHANGED
print("Total amount of orders today is:{} ".format(Total_Amount)) # NUMBER OF ORDERS IN ONE SPECIFIC DAY
answer1 = input("Send another order? (Y/N)").lower()
if Customers[Customer_Name]['Orders'] == 1:
print("This is the first time", Customer_Name, "orders")
Customers[Customer_Name]['TotalAmount'] = Order_Price
else: # TOTAL AMOUNT OF ALL ORDERS DELIVERED
print(Customer_Name, "has spent", Customers[Customer_Name]['TotalAmount'], "in total")
process2 = answer1 == "y"
LengthCustomersList = len(Customer_List)
length += 1
if int(length) == int(LengthCustomersList):
process1 = False
file1.write(username + " has placed {} orders today".format(ORDERS_FROM_EMPLOYEE))
for i in Customer_List:
file.write(i + '\n')
csv_writer = DictWriter(open('data_entered.csv', 'a'),
fieldnames=['Customer Name', 'Customer Address', 'Customer ID', 'Total Orders',
'Total Amount'])
csv_writer.writeheader()
for customer_name in Customers.keys():
csv_writer.writerows(
[{'Customer Name': Customers[customer_name]['Name'],
'Customer Address': Customers[customer_name]['Address'],
'Customer ID': Customers[customer_name]['ID'],
'Total Orders': Customers[customer_name]['Orders'],
'Total Amount': Customers[customer_name]['TotalAmount']}])
openFile()
file.close()
file1.close()
file2.close()
我尝试添加一个Customers[Customer_Name]['Order_Price']
,但它没有工作
解决方案
您的问题与Total_Amount
变量有关。它的值从上一次迭代开始保留并分配给下一个客户。您需要从Customers
字典中检索它,然后更新字典中的值。
if Customer_Name in Customers:
Customers[Customer_Name]['Orders'] += 1
# Customers[Customer_Name]['TotalAmount'] = Total_Amount # This is where the error was
else:
Customers[Customer_Name] = {}
Customers[Customer_Name]['Name'] = Customer_Name
Customers[Customer_Name]['Address'] = Customer_Address
Customers[Customer_Name]['ID'] = uuid.uuid1()
Customers[Customer_Name]['Orders'] = 1
Customers[Customer_Name]['TotalAmount'] = 0
Total_Amount = Customers[Customer_Name]['TotalAmount'] # retrieve value here
Order_Price = float(input("Total amount of order:"))
ORDERS_FROM_EMPLOYEE += 1
print(Customer_Name, "has ordered {} time(s)".format(Customers[Customer_Name]['Orders']))
Total_Amount = Order_Price + Total_Amount
Customers[Customer_Name]['TotalAmount'] = Total_Amount # update value here
推荐阅读
- python - 如何制作一个 tkinter 窗口在被破坏后将停止自我更新?
- c# - 删除 Visual Studio 2019 上的 Source Map 部分
- objective-c - NSArray 和 NSMutableArray 类在内存中占用相同大小吗?
- javascript - 将 .png 文件从库导出到 React JavaScript 项目
- git - 错误:无法锁定配置文件 /etc/gitconfig:权限被拒绝
- c++ - 如何按类名或类型比较两个对象(相当于 C++ 中 Java 的 `getClass()`)
- python - RuntimeError:CUDA 错误:触发了设备端断言 - PyTorch 上的 Resnet18
- python - AttributeError:模块“torch.utils”没有属性“数据”
- python - 如何将生成的数据转换为熊猫数据框
- outlook-2019 - Outlook 2016 2019 随机丢失已发送项目