I'm creating a class that simulates an ATM and I don't know why I'm getting a syntax error on the colon between 'id' and 'zwhite'.
import pickle
import sys
import os
class ATM(object):
def __init__(self):
self.users = ['id':'zwhite','name':'Zack White','pin':'4431','balance':845,
'id':'jzimmerman','name':'John Zimmerman','pin':'6780','balance':59,
'id':'cbenson','Carly Benson','pin':'8991','balance':720]
def check(self,ids):
print Your balance: str(ids['balance'])
def withdraw(self,person):
for i in self.users:
if i['id'] == person['id']:
print Your balance: str(i['balance'])
CodePudding user response:
I suspect you want to be creating a list of dictionaries. This would make sense of the duplicated keys on separate lines:
Try:
self.users = [{'id':'zwhite','name':'Zack White','pin':'4431','balance':845},
{'id':'jzimmerman','name':'John Zimmerman','pin':'6780','balance':59},
{'id':'cbenson','name':'Carly Benson','pin':'8991','balance':720}]
I also added the 'name'
key for the value 'Carly Benson'
in the last row.
CodePudding user response:
You should use a dictionary, not a list.
self.users = {...}
CodePudding user response:
The square brackets ([ ]) are used to create a list. List are used to store elements only. You cannot store keys and values. You can just store elements like [1,2,3,"ab","cd",3.23] etc. If you want to store the data in key-value pairs, then you'll have to use dictionary. By looking at the code, I think you know the concept of dictionary but you're confused with creating dictionary. Curly braces ({ }) are used to create dictionary. Just change the square brackets to curly braces