Python Forum

Full Version: How to do this kind of IF STATEMENT?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to do this kind of IF STATEMENT?
p1=1
p2=0
p3=0
p4=0
pins=[p1,p2,p3,p4]
for i in pins:
    if (pins has value==1)
        print("true")
        
    else:
        print("none")
   
p1=1
p2=0
p3=0
p4=0
pins=[p1,p2,p3,p4]
for value in pins:
    if value == 1:
        print("true")

    else:
        print("none")
Output:
true none none none
(Jun-16-2020, 11:55 AM)menator01 Wrote: [ -> ]
p1=1
p2=0
p3=0
p4=0
pins=[p1,p2,p3,p4]
for value in pins:
    if value == 1:
        print("true")

    else:
        print("none")
Output:
true none none none

thanks , this saves my day! +1
you can do just
if 1 in pins:
   print('true')
else:
   print('none')
or even better
if any(pins):
   print('true')
else:
   print('none')
the best would be
print(any(pins))
Note that having a name for each pin is not best practice
(Jun-16-2020, 12:55 PM)buran Wrote: [ -> ]or even better
if any(pins):
   print('true')
else:
   print('none')

thanks for this, it seems easier. +1