Python Forum
How to do this kind of IF STATEMENT? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to do this kind of IF STATEMENT? (/thread-27668.html)



How to do this kind of IF STATEMENT? - glennford49 - Jun-16-2020

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")
   



RE: How to do this kind of IF STATEMENT? - menator01 - Jun-16-2020

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



RE: How to do this kind of IF STATEMENT? - glennford49 - Jun-16-2020

(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


RE: How to do this kind of IF STATEMENT? - buran - Jun-16-2020

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


RE: How to do this kind of IF STATEMENT? - glennford49 - Jun-17-2020

(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