簡體   English   中英

如何根據用戶在 python 中的輸入打印列表中的某些項目?

[英]How do i print certain items in a list based on user input in 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)

我發現字典在這樣的應用程序中很有用:

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

現在的數據存儲在獨立的列表中。 您必須使用關聯數據類型來實現,例如字典(在 Python 中實現)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM