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.