+ 1
I was writing a code solution for password validation question so what's wrong with my code and it is not giving the desired opt
ps=input() h=['!', '@', '#', '
#x27;, '%', '&', '*'] x=[1,2,3,4,5,6,7,8,9,0] try: if len(h)==2 in ps: pass if len(x)==2 in ps: pass if len(ps)>=7: print("Strong") except: if len(h)<2 in ps: pass if len(x)<2 in ps: pass if len(ps)<7: print("Weak")16 ответов
+ 1
You probably think that this condition is fulfilled like this:
if ( len(ps) >= 7 and x[0] and x[1] and h[0] and h[1] ) in ps:
In fact it is fulfilled like this:
if len(ps) >= 7 and x[0] and x[1] and h[0] and ( h[1] in ps ):
As a result, x[0] and x[1] and h[0] will always be True.
(h[1] in ps) will be True if the @ character is contained in ps.
As a result, you got the condition:
if len(ps) >= 7 and True and True and True and ( h[1] in ps ): ☺️
+ 1
If you use some condition in exception handling, then you must raise a specific exception, otherwise except will not work.
try:
if len(h)==2 in ps: #bug
pass
if len(x)==2 in ps: #bug
pass
if len(ps)>=7:
print("Strong")
else:
raise ValueError()
except:
print("Weak")
You can read in more detail in the course "Intermediate Python", lesson 31.1 "Raise exceptions".
+ 1
How will it work correctly if you haven't fixed any bugs?
+ 1
You need to first count the number of characters h and the number of numbers x contained in ps.
From the received result, compose an output condition.
0
Thanks but it still not working:
ps=input()
h=['!', '@', '#', '#x27;, '%', '&', '*']
x=[1,2,3,4,5,6,7,8,9,0]
try:
if len(h)==2 in ps:
pass
if len(x)==2 in ps:
pass
if len(ps)>=7:
print("Strong")
else:
raise ValueError()
except ValueError:
print("Weak")
0
It is only printing strong
0
O yes I changed my codes totally
0
ps=input()
h=['!','@','&','#','#x27;,'*','%']
if h[0]and h[1]and x[0]and x[1] in ps:
print("Strong")
else:
print("Weak")
0
But it is ignoring the middle code
0
Like if I do it like this:
if len(ps)>=7 and x[0]and x[1]and h[0]and h[1] in ps:
0
It is ignoring the middle code
0
x[0]and x[1]
0
😀
0
😨😱😲🤣🤐
0
😁
- 2