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.