在列表之间转移python

问题描述 投票:-1回答:1

我的代码应该看起来像照片中的结果,我几乎得到了所有东西,但是我陷入了困境。3.转移,如何确定我从输入中获得的清单并更改初始存款?

a = ("#################")

menu = ("%s\n1. Add Client\n2. Show Clients\n3. Transfer\n0. Exit\n%s"%(a,a))

lst = []

def split_list(lst, size):

    for i in range(0,len(lst), size):
        yield lst[i:i + size]

while True:

    print(menu)
    b = input("Select: ")
    if b == "1":
        c = int(input("Client ID: "))
        lst.append(c)
        d = input("Client Name: ")
        lst.append(d)
        e = int(input("Initial Deposit: "))
        lst.append(e)
        print()
        continue

    if b == "2":
        x = list(split_list(lst,3))
        for y in x:
            print(y)
            continue
        print()

    if b == "3":
        x = list(split_list(lst,3))
        f = int(input("Sender ID: "))
        g = int(input("Receiver ID: "))
        h = int(input("How much: "))

    if b == "ilovepython":
        x = list(split_list(lst,3))
        x.sort(key = lambda i:i[2], reverse = True)
        z = x[:3]
        for y in z:
            print(y)
            continue
        print()

    if b == "0":
        print("Exit")
        break

图像

enter image description here

python list for-loop append
1个回答
0
投票

在您的情况下,创建字典(id-键)而不是列表会容易得多。

a = ("#################")

menu = ("%s\n1. Add Client\n2. Show Clients\n3. Transfer\n0. Exit\n%s"%(a,a))

lst = {}


while True:

    print(menu)
    b = input("Select: ")
    if b == "1":
        c = int(input("Client ID: "))
        d = input("Client Name: ")
        e = int(input("Initial Deposit: "))

        lst[c] = [d, e]

        print()

        continue

    if b == "2":
        for i in lst:
            print(i, lst[i][0], lst[i][1])
        print()

    if b == "3":
        SenderID = int(input("Sender ID: "))
        ReceiverID = int(input("Receiver ID: "))
        N = int(input("How much: "))

        if lst[SenderID][1] < N or SenderID not in lst: 
            print("101 CANNOT TRANSFER")
            print()
        else: 
            lst[f] = [lst[f][0], lst[f][1] - h]
            lst[g] = [lst[g][0], lst[g][1] + h]

    if b == "ilovepython":
        topDeposits = {}

        for i in lst:
            if lst[i][1] not in topDeposits:
                topDeposits[lst[i][1]] = [lst[i][0], i]
            else:
                z = topDeposits[lst[i][1]]
                z.append(lst[i][0])
                z.append(i)
                topDeposits[lst[i][1]] = z

        c = 0

        for i in reversed(sorted(topDeposits)):
            for t in range(len(topDeposits[i]) // 2):
                if c < 3:
                    print(topDeposits[i][1 + t * 2], topDeposits[i][0 + t * 2], i)
                    c += 1


        print()

    if b == "0":
        print("Exit")
        break
© www.soinside.com 2019 - 2024. All rights reserved.