0

My program seeks to take user input and find a match in a dictionary. Specifically, the user's input will be the value and once matched the program will print the corresponding key to the value.

My issue now, my modified codes could not accept correct entry beyond first attempt.

I started with the following code:

def get_name(entry):
    customer_id = {'Fayyad':2314,
                'Faysal':9031}
    
    for key, value in customer_id.items():
        if entry == value:
            return f"Welcome {key}!"
        
        return "Sorry, the ID entered is wrong. Try again."
    
    
res = get_name(int(input("Enter your ID: ")))
print(res)

The above spits out perfectly a one-off user input. But I wanna make it more dynamic and interactive where users are allowed a few trials in the event they enter the wrong 'ID' the first attempt.

Therefore, I tried to modify the codes as per below:

def get_name(entry):
    customer_id = {'Fayyad':2314, 'Faysal':9031}
    
    entry_limit = 3

    entry_count = 0

    while entry_count < entry_limit:
        for key, value in customer_id.items():
            if entry == value:
                return f"Welcome {key}!"
        else: 
            int(input("Sorry wrong ID. Please reenter your 4-digit ID number: "))
            entry_count += 1
    
    return "Sorry, you've exceeded your trial limit. Your card will be retained."
        

res = get_name(int(input("Enter your 4-digit ID number: ")))
print(res)

Nonetheless, the output is still not what I desire. It seems that my above codes could not accept the correct entry on second attempt onward.

OUTPUT (correct on first attempt) Enter your ID: 2314 Welcome Fayyad!

Enter your ID: 9031 Welcome Faysal!

OUTPUT (wrong on first attempt) Enter your ID: 2115 Sorry wrong ID. Please reenter your ID: 2314 Sorry wrong ID. Please reenter your ID: 9031 Sorry wrong ID. Please reenter your ID: 2456 Sorry, you've exceeded your trial limit. Your card will be retained.

Frei
  • 1
  • 1
    Remember you can always do this as a straight-forward loop, like `for try in range(0,3):` and skip all the manual counting stuff. – tadman Aug 17 '23 at 15:16
  • You only get one attempt at entering a valid code. If that fails, you prompt for the code again, but you throw away whatever the user typed, and instead try to use the originally-entered code (which is of course going to fail again). – jasonharper Aug 17 '23 at 15:32
  • When you prompt for the re-entry you need to assign to *entry* – DarkKnight Aug 17 '23 at 16:22

3 Answers3

1

You have an else: statement modifying the for-loop. This is legal in python but it is a super-obscure feature, and it almost certainly does not do what you expect. And you don't need it: You just need to wait for the for-loop to terminate (if a return does not intervene). Then save the next input in variable entry, like this:

while entry_count < entry_limit:
    for key, value in customer_id.items():
        if entry == value:
            return f"Welcome {key}!"
     
    entry = int(input("Sorry wrong ID. Please reenter your 4-digit ID number: "))
    entry_count += 1
alexis
  • 48,685
  • 16
  • 101
  • 161
0

Your code is completely correct, but you forgot to put it in the entry variable when the else condition occurs and you get input from the user again:

your code:

else: 
    int(input("Sorry wrong ID. Please reenter your 4-digit ID number: "))
    entry_count += 1

correct code:

else: 
    entry = int(input("Sorry wrong ID. Please reenter your 4-digit ID number: "))
    entry_count += 1
arash.shx
  • 51
  • 2
0

You can simplify this (and make it more robust) by using a reverse lookup dictionary and a common (reusable) function for acquiring the id/PIN

Something like this:

def get_pin(prompt):
    return int(input(prompt))

def get_name(entry):
    customer_id = {'Fayyad':2314, 'Faysal':9031}
    lookup = {v: k for k, v in customer_id.items()} # reverse lookup
    entry_limit = 3

    for _ in range(entry_limit):
        if name := lookup.get(entry):
            return f'Welcome {name}'
        entry = get_pin('Sorry wrong ID. Please reenter your 4-digit ID number: ')
    
    return 'Sorry, you\'ve exceeded your trial limit. Your card will be retained.'

entry = get_pin('Enter your 4-digit ID number: ')

print(get_name(entry))
DarkKnight
  • 19,739
  • 3
  • 6
  • 22