简体   繁体   中英

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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