Python Forum
Dictionaries help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Dictionaries help (/thread-20226.html)



Dictionaries help - Rayaan - Aug-01-2019

My code gives me back all the names in the dictionary when I want it to only give back the name that corresponds with the pin. Any reason why and how I could fix it is appreciated. Thanks!

Here's my code:


import time
count=0
correct_pin = {"Peter":352, "james":123, "richard":321}

while count<4:
    pin = int(input("What is your password: "))

    if pin in correct_pin.values():
       print ("Correct! You are now logged in, ")
       for key in correct_pin.keys():
           print (key)
       time.sleep(1)
       print()
       print()
       print("Opening Codes")
       time.sleep(1)
       print("Please Wait...")
       print()
       time.sleep(3)
       g = open("Codes.txt","r")
       s = g.read()
       print(s)
       g.close()
       exit()
       break


    elif pin not in correct_pin.values() and count<=4:
        print('Wrong! Try again')
        count= count+1

if count<=4:
   print("You got it wrong too many times. Account locked!")



RE: Dictionaries help - ichabod801 - Aug-01-2019

Need a conditional to check the value:

for key, value in correct_pins.items():
    if value == pin:
        print(key)
Note that it would be much easier if you dictionary was reversed: pins for keys, user names for values. Then you can just check if pin in correct_pins: and print the user name with print(correct_pins[pin]).

Of course, you usually get the user name and the pin, and check for the pin matching the user name. Otherwise, the system could only have 10,000 users, one for each pin.


RE: Dictionaries help - perfringo - Aug-01-2019

(Aug-01-2019, 01:37 PM)Rayaan Wrote: My code gives me back all the names in the dictionary when I want it to only give back the name that corresponds with the pin. Any reason why and how I could fix it is appreciated. Thanks!

On row #10 you iterate over all keys in correct_pin and on row #11 print key out.

Additional observation to ichabod801: there is no username validation, authentication is based only on pin. What does it mean? First - one can randomly enter pins and larger the userbase higher the probability that you hit some users pin in 4 guesses. Second - all pins must be unique, otherwise you can't know the user and this leads back to limitation of 10000 users described by ichabod801.


RE: Dictionaries help - Friend - Aug-01-2019

(Aug-01-2019, 02:07 PM)perfringo Wrote: Additional observation to ichabod801: there is no username validation, authentication is based only on pin. What does it mean?

well i saw sth similar in many movies... the agent will enter the PIN and the machine will tell him "Hello Mr. James" :)


RE: Dictionaries help - perfringo - Aug-01-2019

(Aug-01-2019, 02:49 PM)Friend Wrote: well i saw sth similar in many movies... the agent will enter the PIN and the machine will tell him "Hello Mr. James" :)

That's why they are called movies. There is also genre called documentary which resembles real life.


RE: Dictionaries help - Malt - Aug-02-2019

You can have a check like this..
if pin in correct_pin.values():
       print ("Correct! You are now logged in, ")
       for n,k in correct_pin.items():
           if k == pin:
               print(n) #This will print the name which is mapped to the key