Issue
Use case: ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
If the function is passed a valid PIN string, return true, else return false.
My code is working for all the randomly generated pins except for:
'12.0' '1234.0' '111-' '44444-'
def validate_pin(pin):
numbers = '0123456789'
if len(pin) != 4 and len(pin) != 6:
return False
else:
for i in range(len(pin)):
if pin[i] in numbers:
return True
else:
break
return False
Any idea why this would be? These characters do not live within my created variable.
Solution
As kaya3 suggested, the best way to solve issues like this is to step through with a debugger. You can do this in VS code by hitting "Run and Debug" on the debug tab shown below:
Basically your code is checking if the first character is in numbers or not. If it is a number, the for loop breaks, if it isn't, the for loop breaks. Here's a small fix:
def validate_pin(pin):
numbers = '0123456789'
if len(pin) != 4 and len(pin) != 6:
return False
else:
for i in range(len(pin)):
if pin[i] not in numbers:
return False
# if pin[i] in numbers:
# return True
# else:
# break
return True
Answered By - Barnaby
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.