0

I just began teaching myself python today while bored in class so I apologize if my mistake is very simple. I want to have the user type a 4 digit password, then have the program try to crack said password. This is what I have,

import random
from random import *
print("Password Cracker")
password=input("Type a 4 digit password ")
e=password[0]
f=password[1]
g=password[2]
h=password[3]
print("Your password is " + password)
n=0
while True:
  a=randint(0,9)
  b=randint(0,9)
  c=randint(0,9)
  d=randint(0,9)
  n+=1
  if (a==e and b==f and c==g and d==h):
    print("Password Cracked")
    print("Password took " + {4} + " tries to crack")
    break

The a,b,c, and d variables are not working as intended. I was hoping that every time the loop runs they would be updated until eventually, they equal the user inputed password. If anyone can let me know what's wrong with the code that would be much appreciated.

martineau
  • 119,623
  • 25
  • 170
  • 301
Rtrip
  • 1
  • 1
    `e` through `h` are *strings* (length 1, but still strings). Convert each one with the `int` constructor, e.g. `e = int(password[0])` if you want them to be integers that are comparable to the results of `randint`. – ShadowRanger Nov 10 '21 at 02:56
  • 1
    And for printing the number of tries use `print(f"Password took {n} tries to crack")` – Wayne Nov 10 '21 at 03:10

0 Answers0