Problem checking between two python variables

Asked

Viewed 35 times

0

This code is made to generate a password of X random numbers (1 in the case), and then loop random passwords until you get to the initial password, "bruteforce". Although it can generate the first password and the function to generate random passwords also work, the code does not detect when the password generated by the function is equal to the initial password. The problem must be quite trivial, I’m beginner.

import random

low = 'abcdefghijklmnopqrstuvwxyz'

upper = low.upper()

mix = low + upper

password_list = random.sample(mix, 1)

password = ''.join(password_list)


def random_bet():

    try_list = random.sample(mix, 1)

    guess = ''.join(try_list)

    return guess


while random_bet() != password:

    print(random_bet())

    print(password)

    print('Wrong')

    if random_bet() == password:

        print(random_bet(), password)

        print('right')

        break

1 answer

0


Every time you call random_bet() it can return a different value.

Your while and if are "superimposed"

Use as below

bet = random_bet()

while bet != password:
    print(bet)
    print(password)
    print('Wrong')
    bet = random_bet()


print(bet, password)
print('right')

I hope it helps

  • Thank you very much, it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.