idk
idk

Reputation: 50

Word guesser doesn't print enough characters

The idea is that you enter any passwords/characters and then it tries to find that word that you have entered by trying out different combinations of letters and numbers (it really is pointless huh)

The problem right now is that "pw_guess" only prints out one letter or number at a time. There also appears to be duplicates. For example i found that letter "e" was printed 6 times, though should only print it once.

import random

characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
pw = input("Enter a password: \n")
pw_lenght = len(pw)

while True:

    for i in range(pw_lenght):
        random_character = random.randrange(len(characters))
        pw_guess = characters[random_character]
        if pw_guess == pw:
            print('Password found!\nPassword: ', pw)
            exit()
        print(pw_guess)

It is suppose to print and try out as many letter/numbers at a time as how many have been entered in to the user input.

For example: You type "password123" to the input. Then it will count how many characters there are in that user input (11 in this example), and starts trying out and printing different combinations of characters. One print should now include 11 random characters. Then at some point it will get right combination and stop.

As said above, now it only prints one character at a time and there are also duplicate characters which i don't want.

I've tried putting just one letter to the user input, and it guessed it right, so otherwise it works fine.

Upvotes: 1

Views: 100

Answers (3)

LeSchakal
LeSchakal

Reputation: 1

Since Python 3.6 you can use random.choises to get more characters at once.

I would recommend the string-module for better readability.

It's very annoying to fill the terminal with thousands of incorrect guesses, so I've changed the code a little.

import string
import random

characters = string.digits + string.ascii_letters
password = input('Enter a password:\n')
guesses = 1

while True:
    pw_guess = ''.join(random.choices(characters, k=len(password)))
    if password == pw_guess:
        break
    guesses += 1

print('Password found!')
print(f'Password: {pw_guess}')
print(f'{guesses} guesses needed')

Upvotes: 0

John Gordon
John Gordon

Reputation: 33335

Your inner loop should assemble all the random characters into a single password guess, instead of guessing each letter separately.

import random

characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
pw = input("Enter a password: \n")

while True:
    pw_guess = ''
    for i in range(len(pw)):
        pw_guess += random.choice(characters)
    if pw_guess == pw:
        print('Password found!\nPassword: ', pw)
        break
    print('Password not found.  Incorrect guess: ', pw_guess)

Upvotes: 0

Chris
Chris

Reputation: 16147

import random

characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
pw = input("Enter a password: \n")
pw_lenght = len(pw)

while True:

    for i in range(pw_lenght):
        random_character = random.randrange(len(characters))
        pw_guess = ''.join([characters[random.randrange(len(characters))] for x in range(len(pw))])
        if pw_guess == pw:
            print('Password found!\nPassword: ', pw)
            exit()
        print(pw_guess)

Upvotes: 2

Related Questions