0

Heres my code, whenever it is run it skips one of the blocks of code, it doesn't show any error or anything it just skips my first if condition and runs the rest, here it is =

import time

#Cracking PINs

pin = str(input('Type in your 6-digit PIN, it MUST be a number: \n'))
pinLength = len(pin)
solvePin = 0.000002
easyPins = [000000, 0.123456, 0.11111, 0.222222, 0.333333, 0.444444, 0.555555, 0.666666, 0.777777, 0.888888, 0.999999, 0.456123]
pin = int(pin)
pin = pin/1000000

#PIN strength

while(pin in easyPins):
  print('Your PIN is too weak, make another one')
  pin = int(input())

print('Strong PIN!')

#Checking PIN Length and Cracking PIN

startTime = time.time()

if(pinLength == 8):
  confirmPin = int(input('Confirm your PIN \n'))
  confirmPin = confirmPin/1000000
  while(confirmPin != pin):
    print('Not the same PIN\n')
    confirmPin = int(input('Please retype your PIN\n'))
  while(solvePin != confirmPin):
    solvePin += 0.000001
    print(solvePin)
  else:
    while (pinLength != 8):
     int(input('Your PIN is too long or short, type again.\n'))

print(' ')
print('Your Pin is')
print(solvePin)

endTime = time.time()
print(' ')
print ('Elapsed time in seconds', ((endTime - startTime)))
  • What input are you giving it? – James Nov 02 '19 at 21:33
  • 1
    ***"6-digit PIN"*** **is not** `if(pinLength == 8):` – stovfl Nov 02 '19 at 21:36
  • [Floating point numbers (e.g. `0.042`) are for approximate numbers](https://stackoverflow.com/q/21895756/35070), e.g. if you are calculating the distance between places. Integer numbers (e.g. `42`) are appropriate for things you want to count. A PIN is neither; it's a password where all characters happen to be digits. A character string (e.g. `'000042'`) is the appropriate data structure for a PIN. – phihag Nov 02 '19 at 21:40
  • 1
    Please read [how to ask](https://stackoverflow.com/help/how-to-ask). The title of the question should summarize the problem you have. Titles like "please look at my code" are not good. – Valentino Nov 02 '19 at 23:06

1 Answers1

1

You have the line if(pinLength == 8) however you are prompting people to put in a 6-digit pin. So that if is never true.

In addition, the following else block is not correctly indented. It's not lined up with the if(pinLength == 8), so the else never gets called either.

else:
  while (pinLength != 8):
    int(input('Your PIN is too long or short, type again.\n'))

Because this is in the else, even if you fix the indent so it gets called, once called - your script moves on to the end, never going through the verification while loops.

I would actually recommend something like this instead...

import time

easyPins = ["12345678", "87654321"]
#Cracking PINs
while True: # Keeps looping until broken
    rawpin = input('Type in your 8-digit PIN, it MUST be a number: \n')
    if len(rawpin) != 8:
        print("Pin must be 8 digits! Please try again...")
        continue # Restart at the top of the loop
    # Checks if all characters in the pin are the same, circa https://stackoverflow.com/questions/14320909/efficiently-checking-that-string-consists-of-one-character-in-python
    if rawpin  == len(rawpin ) * rawpin [0]:
        print('Your PIN cannot all be the same digit. Please make another one...')
        continue # Restart at the top of the loop
    if (rawpin in easyPins): 
        print("The pin is too easy. Please try again!")
        continue
    # Use a try/except to validate it's all digits
    # This will error and catch if "pin" can't be converted to an int
    try:  
        pin = int(rawpin)
        # pin = int(rawpin)/1000000 # dont need this, see below
    except: 
        print("Pin must be digits only! Please try again...")
        continue # Restart at the top of the loop
    # Do the validation here
    confirmPin = input('PLease re-type your 8-digit PIN: \n')
    if confirmPin != rawpin:
        "The pins do not match!"
        continue # Try again
    else:
        # Dont need to do checks here, since they were done with rawpin
        confirmPin = int(confirmPin)
        break


print('Strong PIN!')
#Checking PIN Length and Cracking PIN
#===============================================================================
# Unfortunately, this will not work, because of the way python does math. 
# The floats come out to a greater decimal than the original inputs. 
# I.e. ...
# 0.1034239999936388
# 0.1034249999936389
# 0.1034259999936389
# 0.1034269999936389
# 0.103427999993639
# 0.103428999993639
# 0.103429999993639
# 0.103430999993639
# 0.1034319999936391
### Original
# startTime = time.time()
# solvePin = 0.000002
# print("pin=", pin)
# print("confirmPin=", confirmPin)
# while(solvePin != confirmPin):
#     print(solvePin)
#     solvePin += 0.000001
# print(solvePin) # This should be outside the loop




# Instead, just compare the numeric values without the math
# Right now, it doesn't matter if the int(pin) is shorter than 8 digits
# You just need them to be the same number
solvePin = 0
startTime = time.time()
for i in range(1, 99999999): # 99999999 is the maximum value of any pin
    solvePin += 1
    if solvePin == pin:
        # leading zeros would be removed when the pin is made into an int
        # However, they will always ONLY be leading zeros that are missing
        # So just pad the string out if needed.
        strpin = str(solvePin)
        while len(strpin) < 8:
            strpin = "0" + strpin
        break
print() # Dont need the ' '
print('Your Pin is:', strpin)

endTime = time.time()
print()
print ('Elapsed time in seconds', ((endTime - startTime)))

OUTPUT:

Type in your 8-digit PIN, it MUST be a number: 
00123123
PLease re-type your 8-digit PIN: 
00123123
Strong PIN!

Your Pin is: 00123123

Elapsed time in seconds 0.04227638244628906
RightmireM
  • 2,381
  • 2
  • 24
  • 42
  • 1
    `int` is not correct in the first place, see [my comment](https://stackoverflow.com/questions/58675477/can-someone-take-a-look-at-my-python-code-its-simple-since-im-only-a-beginner#comment103652239_58675477) Because if I input `000007`, then pinLength should be 6, but if the value is converted to int in between, it will likely come out as pinLength=1. Floating point is even worse, because adding `0.1**pinLength` [will *not* always produce the next PIN](https://stackoverflow.com/q/21895756/35070). – phihag Nov 02 '19 at 21:44