Skip to content

Latest commit

 

History

History
55 lines (28 loc) · 3.14 KB

ktowle2_indentation.md

File metadata and controls

55 lines (28 loc) · 3.14 KB

Investigating Spacing and Indentation in Python

The initial question for this investigation was, "What impact does spacing have on programs in python?" I decided to expand this topic to the role of grammar in naming variables and conditional statements in order to answer that question. This first program simply initializes the variable acceleration in the z axis without using it yet, and sets all pixels to red as long as true is true (forever).

from dcs import*

accel_reading = getAccel (ZAXIS)

while true:

    setAllPixelsTo(RED)

Screen Shot 2018-03-12 at 11.27.31 PM.png

Screen Shot 2018-03-13 at 8.08.54 AM.png

For my more complex program, I introduced a conditional statement. Still defining the acceleration as accel_reading, I also set it equal to zero on start, though I’m not sure if that’s necessary since the sensor should read zero whenever laid flat. Then, I wrote my if statement, followed by a colon, which automatically indented the next line 4 spaces. Using the setAllPixelsToRGB command, I made the pixels yellow when the CPx faces down and blue when it faces up.

from dcs import*

aceel_reading = getAccel (ZAXIS)

accel_reading = 0

if accel_reading > 0:

    setAllPixelsToRGB (255, 155, 0)

else:

    setAllPixelsToRGB (0, 20, 175)

Screen Shot 2018-03-12 at 11.44.43 PM.png

Screen Shot 2018-03-13 at 8.12.40 AM.png

I brought in the element of spacing, which was the original question, into my last program. The program is the same as the last one, and would look the same in Makecode. The only difference is that instead of putting a space before and after each value, they’re all crammed together. Although it doesn’t seem to change the output of the program, this code looks messy and is hard to read. From all of the python we have been exposed to, my brain registers the program above as the “standard” style. I also think this code would be harder to debug because there is no separation.

from dcs import*

accel_reading=getAccel(ZAXIS)

accel_reading=0

if accel_reading>0

    setAllPixelsToRGB(255,155,0)

else:

    setAllPixelsToRGB(0,20,175)

Screen Shot 2018-03-12 at 11.46.20 PM.png

Link to my video on spacing