如何将搜索字段从查找商品ID号转换为根据商品名称查找商品?

用户名

这是我程序的一部分。如何将搜索功能从基于ID号的查找数据转换为基于名为“名称”的项目名称查找项目。这意味着如果我有ID:1,名称:apple,数量:100当前如果我输入1,它将找到所有三个字段的数据。但是,如何更改它以使其接受应用程序(Apple的缩写)并提取所有三个字段?

完整程序代码:

import os
import json

class Inventory:
    def __init__(self):
        #AT LAUNCH GROUPS AND LOADING FUNCTION
        self.items = {}
        self.load()

    def remove(self, ID):
        #REMOVING ITEMS FOR LISTS AND OUTPUT DOCUMENT
        del self.items[str(ID)]
        self.save()

    def add(self, ID, name, qty):
        #ADDING ITEMS FOR LISTS AND OUTPUT DOCUMENT
        self.items[str(ID)] = {"name": name, "qty": qty}
        self.save()

    def update(self, ID, update):
        #UPDATING ITEMS FOR LISTS AND OUTPUT DOCUMENT
        self.items[str(ID)]["qty"] += update
        self.save()

    def search(self, ID):
        #SEARCHING ITEMS FOR LISTS
        item = self.items.get(str(ID), None)
        if item:
            return ID, item['name'], item['qty']
        else:
            return None

    def __str__(self):
        #FORMATTING
        out = ""
        for id, d in self.items.items():
            out += f"ID Number : {id} \nItem Name : {d['name']}\nQuantity : {d['qty']}\n"
            out += "----------\n"
        return out
    
    def save(self):
        #WHERE TO SAVE TO
        with open('data.txt','w') as outfile:
           json.dump(self.items, outfile)

    def load(self):
        #WHERE TO PUT DATA FROM WHEN RELAUNCHING PROGRAM
        try:
            with open('data.txt','r') as json_file:
               self.items = json.load(json_file)
        except:
            print("Can't load old inventory, starting fresh")
            self.items = {}


def menuDisplay():
    #MENU FOR PROGRAM 
    """Display the menu"""
    print('=============================')
    print('= Inventory Management Menu =')
    print('=============================')
    print('(1) Add New Item to Inventory')
    print('(2) Remove Item from Inventory')
    print('(3) Update Inventory')
    print('(4) Search Item in Inventory')
    print('(5) Print Inventory Report')
    print('(99) Quit')


def add_one_item(inventory):
    #ADDING PROMPT AND ERROR CHECKING
    print('Adding Inventory')
    print('================')
    while True:
        try:
            new_ID = int(input("Enter an ID number for the item: "))
            if inventory.search(new_ID):
                print("ID number is taken, please enter a different ID number")
                continue
            new_name = input('Enter the name of the item: ')
            new_qty = int(input("Enter the quantity of the item: "))
            inventory.add(new_ID, new_name, new_qty)
            break
        except Exception as e:
            print("Invalid choice! try again! " + str(e))
            print()


def remove_one_item(inventory):
    #REMOVING PROMPT AND ERROR CHECKING
    print('Removing Inventory')
    print('==================')
    removing = int(input("Enter the item's ID number to remove from inventory: "))
    inventory.remove(removing)


def ask_exit_or_continue():
    #OPTION TO CONTINUE OR QUITE PROGRAM
    return int(input('Enter 98 to continue or 99 to exit: '))


def update_inventory(inventory):
    #UPDATING PROMPT AND ERROR CHECKING
    print('Updating Inventory')
    print('==================')
    while True:
        try:
            ID = int(input("Enter the item's ID number to update: "))
            if inventory.search(ID):
                update = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))
                inventory.update(ID, update)
            else:
                print("ID number is not in the system, please enter a different ID number")
                continue
            break
        except Exception as e:
            print("Invalid choice! try again! " + str(e))
            print()

def search_inventory(inventory):
    #SEARCHING PROMPT AND ERROR CHECKING
    print('Searching Inventory')
    print('===================')
    search = input("Enter the name of the item: ")
    result = inventory.search(search)
    if result is None:
        print("Item not in inventory")
    else:
        ID, name, qty = result
        print('ID Number: ', ID)
        print('Item:     ', name)
        print('Quantity: ', qty)
        print('----------')


def print_inventory(inventory):
    #PRINT CURRENT LIST OF ITEMS IN INVENTORY
    print('Current Inventory')
    print('=================')
    print(inventory)


def main():
    #PROGRAM RUNNING COMMAND AND ERROR CHECKING
    inventory = Inventory()
    while True:
        try:
            menuDisplay()
            CHOICE = int(input("Enter choice: "))
            if CHOICE in [1, 2, 3, 4, 5]:
                if CHOICE == 1:
                    add_one_item(inventory)
                elif CHOICE == 2:
                    remove_one_item(inventory)
                elif CHOICE == 3:
                    update_inventory(inventory)
                elif CHOICE == 4:
                    search_inventory(inventory)
                elif CHOICE == 5:
                    print_inventory(inventory)
                exit_choice = ask_exit_or_continue()
                if exit_choice == 99:
                    exit()
            elif CHOICE == 99:
                exit()
        except Exception as e:
            print("Invalid choice! try again!"+str(e))
            print()

        # If the user pick an invalid choice,
        # the program will come to here and
        # then loop back.


main()
克尔曼07

遍历items字典中的所有id,并检查您的查询是否包含在“ name”值中。

def search(self, query):
    for id in self.items:
        if query in self.items[id]['name']:
            return id, self.items[id]['name'], self.items[id]['qty']
    return None

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将搜索字段从查找商品ID号转换为根据商品名称查找商品?

来自分类Dev

在PayPal付款中获取商品名称

来自分类Dev

映射列表,获取商品名称和内容

来自分类Dev

更改熊猫中的酒吧商品名称

来自分类Dev

修改购物车中的商品名称

来自分类Dev

在Woocommerce购物车商品名称和订单商品名称中添加换行符

来自分类Dev

使用Shell脚本读取文件,并根据商品名称制作2个新文件

来自分类Dev

转到其他页面时无法显示商品名称

来自分类Dev

当商品名称包含“&”符号时,eBay API PHP和XML解析错误

来自分类Dev

WooCommerce-在产品页面之外列出购物车商品名称

来自分类Dev

获取购物车商品名称,数量所有详细信息woocommerce

来自分类Dev

在WooCommerce中电子邮件中表格下方重复的订单商品名称

来自分类Dev

禁用Woocommerce购物车结帐和订单中特定产品的商品名称链接

来自分类Dev

贝宝(Paypal)API NVP错误消息:如果提供了商品类别,则需要商品名称,数量和数量

来自分类Dev

按字符+编号订购商品名

来自分类Dev

如何将商品归类?

来自分类Dev

查找名称相似的商品的最大日期

来自分类Dev

如何将图片与商品ID相关联

来自分类Dev

MYSQL如何根据商店ID订购商品

来自分类Dev

在mongo中,如何查找一组商品,然后添加更多商品以填充所需的商品数量

来自分类Dev

查找具有多个链接的原始商品ID

来自分类Dev

如何将弹性商品的高度设置为与兄弟商品相同的高度

来自分类Dev

通过关联查找商品?

来自分类Dev

如何在商品中向商品添加自定义字段以进行商品比较

来自分类Dev

如何将pcap结构与商品结合使用,包括

来自分类Dev

Woocommerce - 如何将订单和商品匹配回客户

来自分类Dev

ColdFusion亚马逊商品搜索

来自分类Dev

ColdFusion亚马逊商品搜索

来自分类Dev

如何在Vaadin中获取商品ID

Related 相关文章

  1. 1

    如何将搜索字段从查找商品ID号转换为根据商品名称查找商品?

  2. 2

    在PayPal付款中获取商品名称

  3. 3

    映射列表,获取商品名称和内容

  4. 4

    更改熊猫中的酒吧商品名称

  5. 5

    修改购物车中的商品名称

  6. 6

    在Woocommerce购物车商品名称和订单商品名称中添加换行符

  7. 7

    使用Shell脚本读取文件,并根据商品名称制作2个新文件

  8. 8

    转到其他页面时无法显示商品名称

  9. 9

    当商品名称包含“&”符号时,eBay API PHP和XML解析错误

  10. 10

    WooCommerce-在产品页面之外列出购物车商品名称

  11. 11

    获取购物车商品名称,数量所有详细信息woocommerce

  12. 12

    在WooCommerce中电子邮件中表格下方重复的订单商品名称

  13. 13

    禁用Woocommerce购物车结帐和订单中特定产品的商品名称链接

  14. 14

    贝宝(Paypal)API NVP错误消息:如果提供了商品类别,则需要商品名称,数量和数量

  15. 15

    按字符+编号订购商品名

  16. 16

    如何将商品归类?

  17. 17

    查找名称相似的商品的最大日期

  18. 18

    如何将图片与商品ID相关联

  19. 19

    MYSQL如何根据商店ID订购商品

  20. 20

    在mongo中,如何查找一组商品,然后添加更多商品以填充所需的商品数量

  21. 21

    查找具有多个链接的原始商品ID

  22. 22

    如何将弹性商品的高度设置为与兄弟商品相同的高度

  23. 23

    通过关联查找商品?

  24. 24

    如何在商品中向商品添加自定义字段以进行商品比较

  25. 25

    如何将pcap结构与商品结合使用,包括

  26. 26

    Woocommerce - 如何将订单和商品匹配回客户

  27. 27

    ColdFusion亚马逊商品搜索

  28. 28

    ColdFusion亚马逊商品搜索

  29. 29

    如何在Vaadin中获取商品ID

热门标签

归档