0

And operator

I solve a sololearn task called Password Validation Task: Write a program that takes in a string as input and evaluates it as a valid password. The password is valid if it has at a minimum 2 numbers, 2 of the following special characters ('!', '@', '#', '

#x27;, '%', '&', '*'), and a length of at least 7 characters. If the password passes the check, output 'Strong', else output 'Weak'. with this code: password = input() nums = "0123456789" sch = "!@#$%&*" def valid(passw): if len(passw)<7: return "Weak" cnums = 0 csch = 0 for ch in passw: if ch in nums: cnums += 1 if ch in sch: csch += 1 if cnums>=2 and csch>=2: return "Strong" else: return "Weak" print(valid(password)) but when i tried the same code but using bitwise & operator instead of logical and, 2 tests were always failed so i dont understand why didnt bitwise operator worked, as it works quite well with results of comparisons please, explain it to me

11th Jun 2020, 2:37 PM
Аэн Элле
Аэн Элле - avatar
6 odpowiedzi
+ 1
Аэн Элле , Run below code and read the comments you will get idea. cnums = 2 csch = 4 print(cnums>=2 and csch>=2) #below line is executes 2 & csch which 0 so output is False print(cnums>=2 & csch>=2) #if you put bracket as below then first conditions evaluate and & operation performed print((cnums>=2) & (csch>=2)) cnums = 2 csch = 6 print(cnums>=2 and csch>=2) # csch & 2 is not 0 so output is True print(cnums>=2 & csch>=2) print((cnums>=2) & (csch>=2)) cnums = 6 csch = 8 print(cnums>=2 and csch>=2) # 2 and csch is 0 so output is True print(cnums>=2 & csch>=2) print((cnums>=2) & (csch>=2))
11th Jun 2020, 6:23 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
suppose cnums is 5 and csch is 2 in that case & will result in 0
11th Jun 2020, 2:43 PM
Abhay
Abhay - avatar
+ 1
Аэн Элле , Basically in python bitwise operator has most priority then comparision operator. So when you write, C > 2 & B < 4 It will evaluate 2 & B first. Brackets has most priority so after adding bracket it first evaluate condition and then &. https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
12th Jun 2020, 4:01 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
helpful table, seems to me i get why they have such priority thanks for your help :)
12th Jun 2020, 8:34 AM
Аэн Элле
Аэн Элле - avatar
0
thanks, but i still don't understand why in cnums = 4 csch = 2 print cnums>=2 & csch>=2 without parentheses operator & compares cnums and csch but not the result of comparison (cnums>=2 -- True, csch>=2 -- True)
11th Jun 2020, 10:51 PM
Аэн Элле
Аэн Элле - avatar
0
Аэн Элле You are welcome.
12th Jun 2020, 9:19 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar