Code doubt | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
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