This chapter focuses on making indentation and spacing clear using if-statements and while-loops.
For the first program, I made the simplest code I could think of that involved indentation. Its only job was to turn the cpx red forever. In Python, there isn’t a direct “forever” loop, but a while-loop is equivalent. The indentation is clearer in Python, but both programs show that the performance function is inside of the loop. If “setAllPixelsTo(RED)” wasn’t indented, the program wouldn’t run. An error may occur saying “expected indentation,” which is required in order for the program to work.
https://makecode.com/_9DKixdLKf65V
while True:
setAllPixelsTo(RED)
The next program I wrote was slightly more complex. It adds another layer or two of indentation to show the importance of it. When I take “setAllPixelsTo(GREEN)” and move it into the while-loop and outside of the if-statement, the program gets conflicted with multiple true statements happening at the same time. It then tries to perform both functions at once, flashing between the two. I explain this in greater detail in my video.
while True:
while getLux() > 0 and getLux() < 155:
if getLux() > 25 and getLux() < 45:
setAllPixelsTo(RED)
else:
setAllPixelsTo(GREEN)
setAllPixelsToRGB(0,0,0)
As is true in coding, there are multiple ways of creating the same code. The last program is another way of creating the second program I made. It involves less indentation, and it is therefore, in many ways, simpler. Everything is encapsulated in the if-statements. Additionally, I mentioned spaces towards the end of my video. Spaces are important in variable naming, because you cannot have spaces in variable naming. If things have a type of separation, then spaces do not matter. I could put any amount of spaces between a function and the arguments and Python would ignore the spaces and run the program properly.
while True:
if getLux() > 0 and getLux() < 155:
getLux() > 25 and getLux() < 45:
elif:
setAllPixelsTo(RED)
else:
setAllPixelsTo(GREEN)
setAllPixelsToRGB(0,0,0)
Video: https://www.useloom.com/share/231053342fd945e99f78ade51f6ac800