From f94a7aa9e20c23de38da02b3cdfc75a167589925 Mon Sep 17 00:00:00 2001 From: randomuser Date: Fri, 14 Jul 2023 23:29:10 -0500 Subject: [PATCH] passwords v2, with recursion --- passwords.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/passwords.py b/passwords.py index 306e8a3..b8af595 100644 --- a/passwords.py +++ b/passwords.py @@ -10,23 +10,24 @@ passwords = [ final = passwords +class FoundCondition(Exception): + pass + responses = [] for i in range(5): response = [*input("{}th letters: ".format(str(i)))] responses.append(response) +def check(index, word, responses): + if index == 5: + raise FoundCondition(word) + + for a in responses[index]: + if a == word[index]: + check(index + 1, word, responses) + for word in passwords: - foundword = None - for a in responses[0]: - if a == word[0]: - for b in responses[1]: - if b == word[1]: - for c in responses[2]: - if c == word[2]: - for d in responses[3]: - if d == word[3]: - for d in responses[4]: - if d == word[4]: - foundword = word - if foundword: - print(foundword) + try: + check(0, word, responses) + except FoundCondition as e: + print(str(e))