Changes between Version 45 and Version 46 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 6:59:23 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v45 v46  
    5353If you want to do more in a program and actually make decisions in your code, we need to introduce control structures.
    5454
    55 == If-Then-Else ==
     55== If-Then-Else:  Decide, already! ==
    5656Take the program below, it uses an If-Then-Else control structure to ask the program to make a decision, and run code based on that decision.  You'll use these all the time.
    5757
     
    109109(Also note the text in the code that starts with a # sign.  The pound sign and everything after it on that line is called a ''comment'', and its a way to put some notes in the code that the program doesn't run.  The robotics team wants you to put comments in your code a lot--it makes the code easier to read and understand.   And we like it when we can understand you.)   Now for a couple of other control structures that you should know...
    110110
    111 == For Loop ==
     111== For Loop:  Stop Hitting Yourself! ==
    112112The For loop control structure runs code repeats over a variable.  Try running this one:
    113113
     
    132132For loops are handy if you want to avoid repeating yourself in code, and do the same thing over and over again.  The less code you write, the less chance you can make a mistake.  Not to mention its easier to read.
    133133
    134 == While Loop ==
     134== While Loop:  One more time around the block, Alfred! ==
    135135A While loop is the more charming cousin of the FOR loop, this one repeats as long as its condition is ''True''.  If the condition is false when it gets to the While loop, the While loop wont run at all.
    136136
     
    183183Yeah, this is a little harder, but I bet you can do it.  Ask a neighbor for help if you need to.  I'll give you three hints:
    184184
    185  1. You can tell a print to not end with a new line by adding an end="" to the print statement, like this `print("Im a teapot!",end="")`''. ''
     185 1. You can tell a print to not end with a new line by adding an end="" to the print statement, like this `print("Im a teapot!",end="")`''. ''
    186186 1. An empty print statement `print("")` just prints a new line, basically wrapping any line you're on.
    187187 1. If you want to loop over a set of numbers, use a `range()`.  For example, `for x in range(0,5)` will make x go from 0 to 4 incrementing 1 each time it loops.  The first number is where x starts, and the second number is where x will stop __minus 1__.  Don't ask why---its just the way computers and programs work.  Don't believe me?  Fine. [https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm Don't say I didn't warn you.]