Uncategorized

Nested python statements

0

I’m currently learning Python and playing around with some projects on a couple of Pi’s I bought.

I wrote a nested IF statement script that activated LEDs if switches were open or closed. For example if the switch in the first IF statement was closed the red LED came on and stayed on, the script moved to the second IF statement if the switch referenced here was closed the green led came on….and so on.

This is just a bit of fun while I learn about Python, but I thought when the second IF statement is running, if the switch in the first IF were to change the LED wouldn’t react to that change, am I right? Is that a whole new level I’ll learn in the future? Maybe that is outside the capabilities of a nested IF statement?

 New contributor

put on hold as off-topic by joanMilliwaysSteve RobillardDarth Vader 1 hour ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • “This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center.” – joan, Milliways, Steve Robillard, Darth Vader

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    You are correct. The IF statement would need to be executed again for the switch change to be captured. This is a purely Python question, not specific to the Raspberry Pi. – joan 4 hours ago
  • Suppose you have 2 GPIO pins set to input mode, to read buttons, another two GPIO pins output mode, to turn on/off LEDs. Now you can write a “procedural” python program to do two “jobs” in “sequence”:(1) if Switch 1 closed, then turn on Led 1, else do nothing, (2) if Switch 2 closed, then turn on Led 2, else do nothing. Now if python has already completed (1), and now doing (2), checking Switch 2, and at this very point of time Switch 1 changes it mind, will Python bother? The short answer is no, too late for Sw 1 to change its mind, because now is Sw 2’s turn. Long answer is yes, if … . – tlfong01 4 hours ago   
  • If instead of checking switches one by one, you check both at the same time, using the following statements: “If Sw1 closed, AND Sw2 open, then …”, “if Sw1 open and Sw 2 closed, then …”, but this is messy. A ninja python programmer would NOT use nested if/then/else/case/switch statements, because they are ugly, beside tedious, hard to understand, hard to modify. They use ninja tools, such as (1) GPIO pins set to input/interrupt mode, (2) concurrent/parallel programming using python multi-threading/multiporcessing, (3) dictionary data structure instead of logical ctrl procedure, … – tlfong01 3 hours ago   
  • Ok interesting, thanks everyone, way to in-depth for me at this stage, look forward to learning more about it – Jack 30 mins ago

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: