Python Forum
How to do this kind of IF STATEMENT?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do this kind of IF STATEMENT?
#1
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")
   
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What kind of list is this? jesse68 2 1,103 Jun-29-2022, 05:02 PM
Last Post: rob101
  splitting lines, need to add element of same kind tester_V 6 3,072 Feb-20-2021, 11:51 PM
Last Post: tester_V
  What kind of object is this? Moris526 5 2,445 Dec-27-2020, 06:41 AM
Last Post: Gribouillis
  How to write a code to get this kind of output in IDLE kavindu 5 3,496 Oct-28-2018, 07:46 PM
Last Post: wavic
  Newbie question for a kind of rolling average of list of lits zydjohn 3 3,287 Dec-16-2017, 11:41 PM
Last Post: ezdev
  Compiler fault or some kind of weird referencing bug? Joseph_f2 11 9,065 May-09-2017, 08:50 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020