You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不使用字典的情况下实现零件库存初始化与用户输入驱动的库存更新功能(附待完善代码)

Fixing Inventory Update Code Without Using Dictionaries

Got it, let's fix your inventory update code step by step—since we can't use dictionaries, we'll lean on string indexing and a parallel list to track stock levels. Here's how to resolve each of your issues:

1. Get the correct index for a part identifier

Since partIds is a string, you can use the str.index() method to find the position of the selected part (e.g., 'E' in 'ABCDEF' sits at index 4). This index directly maps to the corresponding position in the stockLevel list, which holds our inventory counts.

2. Update the stock level correctly

Your original line partIds[partIdentifier] += quantity is invalid for two reasons: you can't index a string with another character, and partIds stores part codes—not stock values. Instead, use the index we found to modify the stockLevel list, which is where we track actual inventory numbers.

3. Display current and updated stock levels

Once you have the part's index, just pull the value from stockLevel to fill in those [?] placeholders.


Full Corrected Code

def updateInventory():
    startLevel = 100
    partIds = 'ABCDEF'  # Updated to match your requirement of ABCDEF
    stockLevel = [startLevel] * len(partIds)
    
    while True:
        partIdentifier = input("Enter part identifier or <ENTER> to end: ").upper()
        if partIdentifier == '':
            break
        
        # Validate part identifier
        if partIdentifier not in partIds:
            print("The part identifier is invalid")
        else:
            # Get the index of the part in partIds
            part_index = partIds.index(partIdentifier)
            # Print current stock level
            print(f"Current stock level for {partIdentifier} = {stockLevel[part_index]}")
            
            while True:
                try:
                    quantity = int(input("Enter quantity to add: "))
                    if quantity <= 0:  # Updated to block 0/negative values (matches your test example)
                        print("The quantity is invalid")
                    else:
                        # Update the corresponding stock level
                        stockLevel[part_index] += quantity
                        # Print updated stock level
                        print(f"Updated stock level for {partIdentifier} = {stockLevel[part_index]}")
                        break
                except ValueError:
                    # Handle non-integer input errors
                    print("Please enter a valid number")

updateInventory()

Key Modifications Explained

  • Added part_index = partIds.index(partIdentifier) to link part codes to their stock list positions
  • Replaced the invalid partIds[partIdentifier] += quantity with stockLevel[part_index] += quantity to update inventory correctly
  • Filled in the [?] placeholders with stockLevel[part_index] to show accurate stock values
  • Updated the quantity check to quantity <=0 to match your test example where 0 is considered invalid
  • Added a try-except block to handle cases where users enter non-numeric values (a robustness improvement)

Test Run Example

Enter part identifier or ENTER to end: T
The part identifier is invalid
Enter part identifier or ENTER to end: E
Current stock level for E = 100
Enter quantity to add: 0
The quantity is invalid
Enter part identifier or ENTER to end: E
Current stock level for E = 100
Enter quantity to add: 25
Updated stock level for E = 125

内容的提问来源于stack exchange,提问作者zeerock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:37:47