Changes between Version 26 and Version 27 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 5:49:45 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v26 v27  
    5252In lesson 1, we had you create a program, "Hello World".  Now, "Hello World" is not too exciting if you want to program robots, but its an important first step.  We showed you how to run a function (or verb) `print`, which does something (show on the screen the value of its argument `a`, where `a` is assigned the value "Hello World".
    5353
    54 If you want to do more, we need to introduce control structures.  Take the program below.
     54==IF-THEN-ELSE==
     55If you want to do more, we need to introduce control structures.  Take 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.
    5556
    5657{{{
     
    108109(Also note the text 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 team wants you to put comments in your code a lot--it makes the code easier to read and understand)
    109110 
    110 Here's a couple other control structures that you should know
     111Here's a couple other control structures that you should know:
    111112
    112 For Loop:  This one repeats over a variable.  It's good for counting try running this one:;
     113==FOR Loop==
     114The FOR loop control structure runs code repeatly repeats over a variable.  Try running this one:
     115{{{
     116fruits = ['banana', 'apple', 'mango']
     117for fruit in fruits:       
     118   print("Current fruit :", fruit)
     119print("That's alotta fruit!")
     120}}}
    113121
    114 For (Potato
     122It should output:
    115123
    116 While:  The more charming cousin of the for loop, this one repeats
     124{{{
     125Current fruit : banana                                                                                                                                                                                                                                 
     126Current fruit : apple                                                                                                                                                                                                                                 
     127Current fruit : mango
     128}}}
     129
     130This example loops over an ''array'' (a fancy word for a list) of fruits, and prints each value out, each line starting with the text "Current fruit:".  To understand this, pretend that the FOR loop has a big arrow in pointing to the first value in the fruits ''array''.  The first time it runs, this arrow,{{{fruit}}} is pointing to the first value in the ''array'', {{{banana}}}, which it then sets to be the value of the index variable ''fruit''.  After it prints the message, the for loop senses through arcane magic that there are other values in fruits, so it just moves the pointer to the second value and repeats.  After the third fruit, the loop knows there's nothing left so FOR loop exits and prints "That's alotta fruit!"
     131
     132Note that the "Current fruit..." print statement is indented.   Just like in a IF-THEN-ELSE control structure, you can run a block of of multiple lines of code in a for loop, just indent all the lines.
     133
     134FOR 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.
     135
     136==While Loop==
     137
     138A 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. 
    117139
    118140----