0

Code doubt

If I write this: familia=["A","B","C"] familia[0]= "D" if ("A” and "B" and "C") in familia: print ("ok") else: print ("ouch!") print (familia) Output is “ok” (i thought that the second line replace value [0] But I realize that writing this down: familia=["A","B","C"] familia[0]= "D" if ("H” and "B" and "C") in familia: print ("ok") else: print ("ouch!") print (familia) In this case Output is still “ok” I dont understand why is “ok” if “H” is not in familia list...

17th Mar 2021, 4:36 PM
Matias Tardini
Matias Tardini - avatar
6 Respuestas
0
in any statement you should always type the entire phrase, it is not like in human language do. That's the core concept of programming. Yes, it is not possible to resume it or to make it shorter in this case because you used AND operator.
19th Mar 2021, 8:29 AM
iTech
iTech - avatar
+ 2
# yes you can do something like: allin = lambda check, group: all(v in group for v in check) familia = ['A','B','C'] if allin(('A','B','C'),familia): print('ok') else: print('ouch!') # output: ok if allin(('H','B','C'),familia): print('ok') else: print('ouch!') # output: ouch!
19th Mar 2021, 2:57 AM
visph
visph - avatar
+ 1
because you should write: if "A" in familia and "B" in familia and...
17th Mar 2021, 4:39 PM
visph
visph - avatar
+ 1
its because you are missing double equal sign ==, in this case your actual if block will apply the correct one which is "C" in familia, so its ok 🙂 what you should do is <<if "H" in familia and "B" in familia and "C" in familia>> but what you did is if "H" and "B" and "C" in familia which leads to semantic error, the IDE does not show any error but the output is unlogical. I hope you got the point. familia=["A","B","C"] familia[0]= "D" if "H" in familia and "B" in familia and "C" in familia: print("ok") else: print("ouch!") print(familia)
17th Mar 2021, 5:56 PM
iTech
iTech - avatar
0
Matias Tardini familia=list(input().split(" ")) if('A'in familia and'B'in familia and'C' in familia): print("ok") else: print("not ok")
17th Mar 2021, 4:57 PM
Sâgærāvürï
Sâgærāvürï - avatar
0
so this kind of expression cant be resumed? if “A” in familia and “B” in familia “C” in familia... can you say for example This list (list items) in this group...
19th Mar 2021, 12:10 AM
Matias Tardini
Matias Tardini - avatar