-3
print("Please confirm your identity user(lower case):")
pins = {"abra":9999,"anna":1888}
name = input("")
print("Now please confirm you pin:")
pin = int(input(">>"))
if pin == pins.value[0] and name == pins.keys[0]
    print("You are great")
elif pin == pins.value[1] and name == pins.keys[1]
    print("you are dumb")

Now what i want to do here is to match the inputs of the user with the dictionary that i have already saved in memory. Please help me with the functions that would help me do so.Also , please suggest me any other way with which I can do so.

2 Answers2

2

You get the name, I'd just get the pin using that:

if pins[name] == int(pin) #input gives you a string

this will throw an exception if the naem is not in pins, so you can catch that and use it to tell the user, that the name is wrong.

Alternatively you can use the safe pins.get(name), will give you none if the name not exists.

>pins = {"abra":9999,"anna":1888}
>pin = input("")
1888
>int(pin) == pins['anna']
True

Bernhard
  • 1,253
  • 8
  • 18
-2

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"

Swift
  • 1,663
  • 1
  • 10
  • 21
  • I might be missunderstanding something here, but I think that's an inefficient way of handling this, since you iterate the whole dict comparing each key with the given name. This destroys the quick lookup times dicts usually provide. – Bernhard Jul 04 '18 at 11:19
  • I think you misunderstand the loop, notice the `break` This means break the for loops. Also meaning that if there is a positively matching pin and username, the loop will stop. – Swift Jul 04 '18 at 16:15
  • Ofc, but for some of the values you iterate the whole thing, on average you'll iterate half the dict every time you look a user up, which makes looking up a value in it basically O(n) like for lists. While checking directly via key for a value is more like O(1) (independent from the dict lenght) through the optimization of dicts via hash tables. – Bernhard Jul 05 '18 at 05:23
  • I must disagree and scald the downvote. In your question you asked "What other methods could be used?" My answer is a perfectly reasonable and simple answer which could have helped both you and others seeking a similar answer. It disappoints me that the silly culture of downvoting for no reason continues unhindered on SO – Swift Jul 05 '18 at 14:22
  • And to add further, please note: I have tested the functions with two seperate usernames and pins. AS in the example above, "testname14" was used with the pin 1248. I have also tested on my PC, "testuser99" with pin 1333. On both occasions, the time taken to check was less with test_func_2 and test_func_1 performed slower. – Swift Jul 05 '18 at 15:11