I'm a beginner at python programming. I am wondering how I would use users input to reference the "Account number" key then print the correct dictionary for that key.
#Dictionary entry for John
print("\n")
key123 = {
"Account number": "123",
"First Name": "John",
"Last Name": "Hutch",
"Savings Amount": 1964,
"Checkings Amount": 6437
}
#Dictionary entry for Suzie
key456 = {
"Account number": "456",
"First Name": "Suzie",
"Last Name": "Gonzalez",
"Savings Amount": 2001,
"Checkings Amount": 1256
}
#Dictionary entry for Zeke
key789 = {
"Account number": "789",
"First Name": "Zeke",
"Last Name": "Pena",
"Savings Amount": 1200,
"Checkings Amount": 2313
}
Each entry against a user seems to be a dictionary. So you can make a list of dictionaries and try filtering using the filter function in python. ( As the key on which you want to filter is the same in all the dictionaries in the list, a simple filter function can give you the desired result)
Let's consider you want to filter on basis of Account_Number = "456"
user_input = "456"
user_details = [
{
"Account number": "456",
"First Name": "Suzie",
"Last Name": "Gonzalez",
"Savings Amount": 2001,
"Checkings Amount": 1256
},
{
"Account number": "789",
"First Name": "Zeke",
"Last Name": "Pena",
"Savings Amount": 1200,
"Checkings Amount": 2313
}]
res = list(filter(lambda x: x["Account number"] == user_input, user_details))
print(res)
[{'Account number': '456', 'First Name': 'Suzie', 'Last Name': 'Gonzalez', 'Savings Amount': 2001, 'Checkings Amount': 1256}]
If you further want to shorten your results or want to get only a few fields out of the dictionary you can use the map function as below:-
mapOutput = list(map(lambda x: [x["First Name"], x["Last Name"]], res))
print(mapOutput)
[['Suzie', 'Gonzalez']]
Hope you got the solution. Please do reach out in the comment section if you need further clarification, will be happy to help.
You can create list
of dict
.
data = [
{
"Account number": "456",
"First Name": "Zeke",
"Last Name": "Pena",
"Savings Amount": 1200,
"Checkings Amount": 2313
},
"Account number": "456",
"First Name": "Suzie",
"Last Name": "Gonzalez",
"Savings Amount": 2001,
"Checkings Amount": 1256
}
]
Add all of your data in a list.Then search in it with filter
.
filter(lambda x: x["Account number"] == key_input, data)
what you need is to use input()
to get the user input, then scan through the accounts to match the account number
accounts = {
'key123' : { #Dictionary entry for John
"Account number": "123",
"First Name": "John",
"Last Name": "Hutch",
"Savings Amount": 1964,
"Checkings Amount": 6437
},
'key456' : { #Dictionary entry for Suzie
"Account number": "456",
"First Name": "Suzie",
"Last Name": "Gonzalez",
"Savings Amount": 2001,
"Checkings Amount": 1256
},
'key789' : { #Dictionary entry for Zeke
"Account number": "789",
"First Name": "Zeke",
"Last Name": "Pena",
"Savings Amount": 1200,
"Checkings Amount": 2313
}
}
inp = input("enter account number: ")
list(filter(lambda x: x["Account number"] == inp, accounts.values()))
Output
enter account number: 123
[{'Account number': '123',
'Checkings Amount': 6437,
'First Name': 'John',
'Last Name': 'Hutch',
'Savings Amount': 1964}]
I was able to get a correct answer. I was able to use this code:
user_input = input("What is your account number?")
user_details = [
{
"Account number": "123",
"First Name": "John",
"Last Name": "Hutch",
"Savings Amount": 1964,
"Checkings Amount": 6437
},
{
"Account number": "456",
"First Name": "Suzie",
"Last Name": "Gonzalez",
"Savings Amount": 2001,
"Checkings Amount": 1256
},
{
"Account number": "789",
"First Name": "Zeke",
"Last Name": "Pena",
"Savings Amount": 1200,
"Checkings Amount": 2313
}]
res = list(filter(lambda x: x["Account number"] == user_input, user_details))
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.