I would use a different method.
for key, val in pins.items():
if name == key:
if val == pin:
if name == 'abra':
print('You are great!')
break
if name == 'anna':
print('You are dumb!')
break
else:
print('Incorrect username or pin, please try again.')
For clarity:
pins.items()
will unpack into the given keywords "key"
& "val"
so you can then iterate over the entire dictionary.
This will help if you added 100 users to your dictionary. It would mean the only thing you need to change would be the welcome messages depending on the user.
You could go one extra and make a list of insults and then randomly choose one from the list if Anna logged in. Google random.choice()
as that isn't related to your original question.
The function/output for your method:
def test_func_1(db=userpins):
start_time = time.time()
name = 'testname14'
pin = 1248
if db[name] == pin:
print('authenticated!')
print("--- %s seconds ---" % (time.time() - start_time))
Output:
test_func_1()
authenticated!
--- 0.048806190490722656 seconds ---
And the function / output for my function:
def test_func_2(db=userpins):
start_time = time.time()
db = db.items()
name = 'testname14'
pin = 1248
for user, pin_c in db:
if user == name:
if pin_c == pin:
print('authenticated')
print("--- %s seconds ---" % (time.time() - start_time))
Output:
test_func_2()
authenticated
--- 0.048050880432128906 seconds ---
Not sure if your comment really is valid anymore, because quite clearly, the function seems to be faster.
I had to tweak it slightly. So, rather than unpack during the for loop, we do it just before we begin the loop. We then iterate over a list of names, matching the name only, if the name matches, we can then check the pin.
Hope you decide to change your vote, considering I took the time to show you why you are incorrect.
I also took the liberty of making the "pins" dictionary 50 times as long. I will give you that list here, in-case you would like to test them:
{'testname1': 1235, 'testname2': 1236, 'testname3': 1237, 'testname4': 1238, 'testname5': 1239, 'testname6': 1240, 'testname7': 1241, 'testname8': 1242, 'testname9': 1243, 'testname10': 1244, 'testname11': 1245, 'testname12': 1246, 'testname13': 1247, 'testname14': 1248, 'testname15': 1249, 'testname16': 1250, 'testname17': 1251, 'testname18': 1252, 'testname19': 1253, 'testname20': 1254, 'testname21': 1255, 'testname22': 1256, 'testname23': 1257, 'testname24': 1258, 'testname25': 1259, 'testname26': 1260, 'testname27': 1261, 'testname28': 1262, 'testname29': 1263, 'testname30': 1264, 'testname31': 1265, 'testname32': 1266, 'testname33': 1267, 'testname34': 1268, 'testname35': 1269, 'testname36': 1270, 'testname37': 1271, 'testname38': 1272, 'testname39': 1273, 'testname40': 1274, 'testname41': 1275, 'testname42': 1276, 'testname43': 1277, 'testname44': 1278, 'testname45': 1279, 'testname46': 1280, 'testname47': 1281, 'testname48': 1282, 'testname49': 1283, 'testname50': 1284, 'testname51': 1285, 'testname52': 1286, 'testname53': 1287, 'testname54': 1288, 'testname55': 1289, 'testname56': 1290, 'testname57': 1291, 'testname58': 1292, 'testname59': 1293, 'testname60': 1294, 'testname61': 1295, 'testname62': 1296, 'testname63': 1297, 'testname64': 1298, 'testname65': 1299, 'testname66': 1300, 'testname67': 1301, 'testname68': 1302, 'testname69': 1303, 'testname70': 1304, 'testname71': 1305, 'testname72': 1306, 'testname73': 1307, 'testname74': 1308, 'testname75': 1309, 'testname76': 1310, 'testname77': 1311, 'testname78': 1312, 'testname79': 1313, 'testname80': 1314, 'testname81': 1315, 'testname82': 1316, 'testname83': 1317, 'testname84': 1318, 'testname85': 1319, 'testname86': 1320, 'testname87': 1321, 'testname88': 1322, 'testname89': 1323, 'testname90': 1324, 'testname91': 1325, 'testname92': 1326, 'testname93': 1327, 'testname94': 1328, 'testname95': 1329, 'testname96': 1330, 'testname97': 1331, 'testname98': 1332, 'testname99': 1333}
Here is a screenshot of the results of "testuser99"