无法追加数组。

huangapple go评论48阅读模式
英文:

unable to append an array

问题

抱歉,我无法提供代码的翻译。如果您有其他需要翻译的文本,请随时提问。

英文:

I know it is a heck a lot of code but hear me out, in the option coonfigure items when i add an item to the array 'items' and print it the same time it works perfectly but when i got to view all the items i see the array is empty.I am a python beginner so you will see some rookie mistakes me being lazy.

the main code where the problem is under stars

z = 0
name = []
debts=[]
debt = 0
j=' '
def name_search(arr, target):
    for j in range(len(arr)):
        if arr[j] == target:
            return print("Name has been created already")
    return name.append(n)

k=0
def linear_search(arr, target):
    for k in range(len(arr)):
        if arr[k] == target:
            return k
            return "Item has not been created or spelling is wrong"
for z in range(1, 10000000000):
    print("\n\nWelcome to Canteen software\nPlease choose one of the options given below:\n1. Create a bill\n2. Debts\n3. Configure Items\n4. Exit\n")
    i = int(input())
    items = []
    prices = []
    if(i == 1):
        print("Create a bill\nName of person:")
        n = input()
        name_search(name, n)
        print(name)
        print("Items Bought")
        for i in range(1, 100):
            item = input()
            result =linear_search(items, item)
            print("Enter the number of items bought")
            num = int(input())
            print(num, item, "has been added")
            print("Type 1 to add item and 2 to move on")
            i2 = int(input())
            if(i2 == 1):
                print("Adding New Item")
            else:
                break
        print("Total Cost")
        print("The price of 1", item, "is", prices[result])
        total_cost = prices[result] * num
        print("The total cost is", total_cost)
        print("Amount paid in cash:")
        paid = int(input())
        print("Debt")
        debt = 0
        debt = total_cost - paid
        print(debt)
        dr=linear_search(name,n)
        l=len(name)
        if(l-1==dr):
            debts.append(debt)
        elif(l!=dr):
            debts[dr]=debts[dr]+debt
    elif(i==2):
        print("Debts\nType 1 to search a name or type 2 to see all debts")
        v=int(input())
        if(v==1):
            print("Enter the name of the customer(make sure the spelling is correct)")
            c=input()
            result=linear_search(name,c)
            print(debts[result])
        elif(v==2):
            print(name)
            print(debts)
        else:
            print("Wrong choice try again")
    **elif(i==3):
        print("Configure items")
        print("Type 1 to create item and type 2 to view all items")
        o=int(input())
        if(o==1):
            print("Enter the name of the item")
            item=0
            item=input()
            items.append(item)
            print(items)
            print("Enter the price of the item")
            price=int(input())
            prices.append(price)
            print(prices)
            print("Item has been added")
        elif(o==2):
            print("All items")
            print(items)
            print(prices)**
        else:
            print("Wrong choice try again")
    elif(i==4):
        print("Thank you for using my software:)")
        quit()
    else:
        print("Wrong choice try again")

答案1

得分: 0

你在for循环内部设置了items = []。这意味着你在每一步的for循环中都分配了一个空数组。就像你对其他列表所做的那样,你应该在for循环之前初始化你的items列表。这应该能解决你的问题。

英文:

You are setting items = [] inside of your for-loop. That means you're assigning an empty array in each step of your for-loop. Just like you did with your other lists, you should initialize your items list before the for-loop. That should fix your problem.

huangapple
  • 本文由 发表于 2023年6月18日 21:45:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定