简体   繁体   English

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

[英]How do i print certain items in a list based on user input in python?

So far the user is supposed to enter their name account number and balance which gets put into list.到目前为止,用户应该输入他们的姓名帐号和余额,然后将其放入列表中。 then I ask for them to input their account number and im supposed to print out their specific number from the list instead of the whole list but cant get it to work.然后我要求他们输入他们的帐号,我应该从列表而不是整个列表中打印出他们的特定号码,但无法让它工作。


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)

I find dictionaries are useful in applications like this:我发现字典在这样的应用程序中很有用:

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"])

The data right now is stored in independent lists.现在的数据存储在独立的列表中。 You would have to implement using an associative data type such as dictionaries (that are implemented in Python).您必须使用关联数据类型来实现,例如字典(在 Python 中实现)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM