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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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")

1st Aug 2021, 3:31 AM
Vaibhav Tiwari
16 Answers
+ 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 ): ☺️
1st Aug 2021, 5:25 PM
Solo
Solo - avatar
+ 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".
1st Aug 2021, 5:57 AM
Solo
Solo - avatar
+ 1
How will it work correctly if you haven't fixed any bugs?
1st Aug 2021, 9:16 AM
Solo
Solo - avatar
+ 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.
1st Aug 2021, 1:22 PM
Solo
Solo - avatar
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")
1st Aug 2021, 8:57 AM
Vaibhav Tiwari
0
It is only printing strong
1st Aug 2021, 8:58 AM
Vaibhav Tiwari
0
O yes I changed my codes totally
1st Aug 2021, 10:57 AM
Vaibhav Tiwari
0
ps=input() h=['!','@','&','#','
#x27;,'*','%'] if h[0]and h[1]and x[0]and x[1] in ps: print("Strong") else: print("Weak")
1st Aug 2021, 10:58 AM
Vaibhav Tiwari
0
But it is ignoring the middle code
1st Aug 2021, 10:58 AM
Vaibhav Tiwari
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:
1st Aug 2021, 11:00 AM
Vaibhav Tiwari
0
It is ignoring the middle code
1st Aug 2021, 11:00 AM
Vaibhav Tiwari
0
x[0]and x[1]
1st Aug 2021, 11:01 AM
Vaibhav Tiwari
0
😀
2nd Aug 2021, 1:40 AM
Vaibhav Tiwari
0
😨😱😲🤣🤐
2nd Aug 2021, 8:11 AM
Solo
Solo - avatar
0
😁
3rd Aug 2021, 2:20 AM
Vaibhav Tiwari
2nd Aug 2021, 3:21 AM
Vaibhav Tiwari