Python 如何添加库存剩余计数器?
以前,我曾请求帮助尝试添加一个不起作用的库存计数器。这是我制作的原始程序。一切正常。如何设置每个项目(裤子、衬衫、连衣裙、袜子和毛衣)的数量为100,然后输入数量并减去该数字。最后,在菜单的选项2中打印您的账单后,打印剩余的100。请编辑代码以帮助我解决此问题。我真的很想完成这个计划。谢谢你抽出时间Python 如何添加库存剩余计数器?,python,Python,以前,我曾请求帮助尝试添加一个不起作用的库存计数器。这是我制作的原始程序。一切正常。如何设置每个项目(裤子、衬衫、连衣裙、袜子和毛衣)的数量为100,然后输入数量并减去该数字。最后,在菜单的选项2中打印您的账单后,打印剩余的100。请编辑代码以帮助我解决此问题。我真的很想完成这个计划。谢谢你抽出时间 #DEFINING EACH RETAIL ITEM TEMPLATE class RetailItem: def __init__(self,desc,unit,price):
#DEFINING EACH RETAIL ITEM TEMPLATE
class RetailItem:
def __init__(self,desc,unit,price):
self.description = desc
self.units = unit
self.price = price
#CREATING WHERE SELECTED ITEMS WITH RESIDE
class CashRegister:
def __init__(self):
self.shopping_list=[]
def purchase_item(self,retail):
#adding item to list
self.shopping_list.append(retail)
#CREATING PRICING / COST
def get_total(self):
total = 0
for i in self.shopping_list:
total+=(i.units*i.price)
return total
#HOW THE CHECKOUT WILL FORMAT/LOOK IN AFFECT ON OPTION 2 (CHECKOUT)
def show_items(self):
if not self.shopping_list:
return
print("{:<15}{:<25}{:<25}{:<10}".format(" "*9,"Description","Units","Price"))
print("-"*75)
for c,i in enumerate(self.shopping_list,1):
print("{:<15}{:<25}{:<25}{:<10}".format("Item #"+str(c),i.description,i.units,i.price))
print("-"*75)
def main():
#ADDING TO CART
c=CashRegister()
#MENU
while 1:
try:
print("1. Purchase\n2. Checkout\n3. Clear\n4. Exit")
print()
choice = int(input("Enter your choice: "))
print()
if choice == 1:
while 1:
print()
print("Menu\n-----------------------\n1. Pants\n2. Shirt\n3. Dress\n4. Socks\n5. Sweater")
item_type = int(input("Select item: "))
print()
qty=int(input("Enter quantity: "))
#ITEMS TO CHOOSE FROM AND ADD TO CART
if item_type == 1:
c.purchase_item(RetailItem("Pants",qty,44.95))
elif item_type == 2:
c.purchase_item(RetailItem("Shirt",qty,34.95))
elif item_type == 3:
c.purchase_item(RetailItem("Dress",qty,79.95))
elif item_type == 4:
c.purchase_item(RetailItem("Socks",qty,6.95))
elif item_type == 5:
c.purchase_item(RetailItem("Sweater",qty,24.95))
else:
#ERROR MESSAGE
print("Invalid item! try again")
print()
continue
print("Item added to list")
print()
break
elif choice == 2:
if not c.shopping_list:
#ERROR MESSAGE
print("Nothing to checkout!")
print()
continue
#DISPLAYING BILL
c.show_items()
print(" "*60+"Total: ${}".format(round(c.get_total(),2)))
print()
elif choice == 3:
#CLEARING
c.shopping_list=[]
elif choice == 4:
#EXIT
exit()
else:
#ERROR MESSAGE
print("Invalid choice! try again!")
print()
#ERROR MESSAGE
except Exception as e:
print("Invalid choice! try again!"+str(e))
print()
#RUN PROGRAM
if __name__ == "__main__":
main()
#定义每个零售商品模板
类别零售项目:
定义初始(自我、描述、单位、价格):
self.description=desc
self.units=单位
self.price=价格
#创建包含选定项目的位置
类别收银机:
定义初始化(自):
self.shopping_list=[]
def采购项目(自有、零售):
#将项目添加到列表中
self.shopping\u list.append(零售)
#创建定价/成本
def get_总计(自身):
总数=0
对于self.shopping_列表中的i:
总计+=(i.单位*i.价格)
返回总数
#签出的格式/外观如何影响选项2(签出)
def显示_项(自身):
如果不是自助购物清单:
回来
打印(“{:您从未定义项目类型数量
。您在课堂上使用它所做的一切是:
self.item_type_qty[["Pants",20],["Dress",20]]
这没有任何效果,因为看起来您正试图使用奇怪的索引获取一个值。如果您想分配它,请按以下方式执行:
self.item_type_qty = [["Pants",20],["Dress",20]]
但是,这一行会遇到另一个问题:
for x in c.item_type_qty:
该行在调用inhand
之前运行,这意味着当到达该行时,item\u-type\u-qty
仍将是未定义的,无论如何都会抛出该错误。您有一种循环依赖关系,您尝试循环通过item\u-type\u-qty
,这是由inhand()设置的
,但在循环中,您使用x[0]
和x[1]调用inhand()
,但是x
是项目类型数量
中的一个项目,该项目在inhand
中定义。只是一种混乱的情况,没有任何意义。请提供预期的结果。显示中间结果与预期的不同之处。我们应该能够复制并粘贴连续的代码块,执行t文件,并重新生成您的问题以及跟踪问题点的输出。这使我们可以根据您的测试数据和所需的输出测试我们的建议。您发布的代码挂起,等待输入。代码不是最小的。请包含整个错误消息,并确保跟踪有问题的值。