-1

To refresh my basic Python after a break of several years, I am trying to solve a password verification problem by scholar technique. According to my task, I have to (1) write a function; (2) use some other functions pre-written by me; (3) use different techniques for each function; (4) use while loop to check the password for certain characters, step by step; (5) do not use advanced techniques like regex etc. and do not write tests.

The problem is: if I input a string which should pass the condition b "char_in_str(inp) != True" (for instance, by typing j8& into the command line), at the first time I get no char, but if I input exactly the same j8& at the second time, it works as expected and I see YES.

What am I missing in my code? It's cool if you point out where to look instead of writing a straightforward solution. If I can't fix the code, I'd rather ask for a solution additionally))

Here is my code:

# -*- coding: utf-8 -*-
       
def num_in_str(inp):
    return any(s.isdigit() for s in inp)

# it should be stopped on j
# it should pass j8

def char_in_str(inp):
    set_s = set(inp)
    set_ch = {"№", "#", "%", "!", "$", "@", "&", "*"}
    for s in set_s:
        if s in set_ch:
            return True
        else: 
            return False

# it should be stopped on j8
# it should pass j8&


def password():
    inp = input("Please enter your password here: ")
    a = num_in_str(inp) != True
    b = char_in_str(inp) != True

    incorrect = (a or b)
    while incorrect:
        if a:
            print("no num")    
            inp = input("here: ")
            break
        elif b:
            print("no char")
            inp = input("here: ")
            break
        
    print("YES")
                          

password()

I see on the terminal:

Please enter your password here: j8&
no char
here: j8&
YES

For this task, I use the Visual Studio Code (it is a prerequisite). I do not use it normally and have no idea if it has its own features influencing the result.

kokserek
  • 530
  • 8
  • 21

1 Answers1

1

Look at your function. You read inp, you evaluate it. Then, in that pointless while loop, you print a result, you fetch another input, and you break from the loop, which prints "YES" and exits. You need to ask for the input exactly once, and that needs to be inside the loop:

def password():
    while True:
        inp = input("Please enter your password here: ")

        if not num_in_str(inp):
            print("no num")    
        elif not char_in_str(inp):
            print("no char")
        else:
            break
        
    print("YES")

Your char_in_str function only checks the first character. Change the last couple of lines:

def char_in_str(inp):
    set_ch = "№#%!$@&*"
    for s in inp:
        if s in set_ch:
            return True
    return False
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • 1
    That's because your `char_in_str` only checks the first character. "&g5" will work. I'll show you how to fix that. – Tim Roberts Aug 06 '21 at 06:44
  • Thank you, it works!) Of course only one input)) So stupid mistakes((( Your support is really great! I deleted my first comment unintentionally, so, now only I am able to use your comment, sorry for this. – kokserek Aug 06 '21 at 09:19
  • And it would be great if you show me how you fix that. I would like to learn from you. Please, show me? )) – kokserek Aug 06 '21 at 09:26
  • Yes, your solution works and it's elegant)) Great! and I fully understand my mistakes now... I hope so) – kokserek Aug 06 '21 at 10:48