Ask coding questions

← Back to all posts
Why isn't this working
Wilke000 (625)

@IndyRishi and I are working on this password breaker, and we can't get it to accept 0000 or anything like that. Please Help!

Comments
hotnewtop
RYANTADIPARTHI (6000)

just replace str with int on line 1.

pswrd = int("Enter 4-digit password:\t\n")

like that.

RYANTADIPARTHI (6000)

@Wilke000 why are you reinitializing it on line 2?

mesquite2234 (278)

remove or len(str(pswrd)) > 4

19ecal (228)

This is because as you are storing passwords in a int, python automatically removes any leading zeros, so the length isn't 4 any more. To solve this use itertools.product instead

digits="0123456789"
pswrd=""
while len(pswrd)!=4:
  pswrd=input("Enter password ")
for i in itertools.product(digits, repeat=4):
  if i==pswrd:
    print(f"Guessed in {i} tries")
  else:
    print(f"Failed password {i}")
Wilke000 (625)

ok @19ecal, thanks! i'll try it

Wilke000 (625)

interpools is an error @19ecal, do I need to import something?

19ecal (228)

@Wilke000 Yes I forgot you need to import itertools

19ecal (228)

@Wilke000 Ok here's the code, I actually forgot some things the first time

import itertools
digits="0123456789"
pswrd=""
while len(pswrd)!=4:
  pswrd=str(input("Enter password "))
for i in itertools.product(digits, repeat=4):
  ipswrd="".join(i)
  if ipswrd==pswrd:
    print(f"Guessed in {ipswrd} tries")
    break
  else:
    print(f"Failed password {ipswrd}")
Baconman321 (1059)

I'm assuming that "0001" gets converted to "1".

Try checking the length of the raw input, then converting it to a number.