如何根据python中的用户输入在列表中打印某些项目?

时间:2018-11-09 02:11:18

标签: python

到目前为止,应该由用户输入其名称帐号和余额,并将其输入列表。然后我要求他们输入他们的帐号,我应该从列表中而不是整个列表中打印出他们的特定号码,但是无法正常工作。


customers = list()
acctnum = list()
balance= list()

count = 0
while count != 2: 
    name = input("Enter your name: ")
    customers.append(name)
    num = int(input("Enter account number: "))
    acctnum.append(num)
    bal = input("Enter Current Balance:$ ")
    print()
    balance.append(bal)
    count = count + 1

print(customers)
print(acctnum)
print(balance)


personal = int(input("Enter account number: "))
print(personal)
if personal in acctnum:

   print(acctnum)

2 个答案:

答案 0 :(得分:0)

我发现字典在这样的应用程序中很有用:

accounts = {}

for x in range(2): 
    name = input("Enter your name: ")
    num = int(input("Enter account number: "))
    bal = input("Enter Current Balance: $")
    accounts[num] = {"name": name, "balance": bal}
    print()

for k, v in accounts.items():
    print(v["name"] + "'s account " + str(k) + " has $" + v["balance"])

personal = int(input("Enter account number: "))
if personal in accounts:
    print("Hello, " + accounts[personal]["name"] + " you have $" + accounts[personal]["balance"])

答案 1 :(得分:0)

现在的数据存储在独立的列表中。您将必须使用诸如字典(在Python中实现)之类的关联数据类型来实现。