Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionaries help
#1
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!")
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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" :)
Reply
#5
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020