Changes between Version 31 and Version 32 of ControlSystems/SoftwareTeam/IntroToPython


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

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v31 v32  
    4444Hello World!
    4545}}}
    46 The program told the computer to print the value of variable a on the screen. 
     46The program told the computer to print the value of variable a on the screen.
    4747
    4848And, boom, now you're a programmer.  The rest of learning how to code is just more details.  This is the end of Lesson 1.
    4949
    50 
    51 = Lesson 2: Making decisions  =
     50= Lesson 2: Making decisions =
    5251In 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".
    5352
    54 If you want to do more, we need to introduce control structures. 
     53If you want to do more, we need to introduce control structures.
    5554
    5655== If-Then-Else ==
     
    6867print("Hope you had a good time")
    6968}}}
    70 
    7169''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    7270
     
    8078Hope you had a good time                                                                                                                                                                                                                               
    8179}}}
    82 
    8380Three things to note about this program:
    8481
     
    8784 1. You'll see that a few of the print statements end with a {{\\n}}.  This is called a ''escape sequence'', and its shorthand for a newline.  When the computer sees it in a print statement, it will add an extra line to the output. See here for other escape sequences you can add.
    8885
    89 Now, in the editor replace the line with the if ''expression'' with
     86Now, in the editor replace the line with the if ''expression'' with
     87
    9088{{{
    9189if location == 'Poughkeepsie, NY':
    92 }}}
    93 
     90}}}
    9491and run it again.   What happens?   It should print "Go away, we're out eating tacos".  Why?  If I was in Poughkeepsie, I'd be out eating tacos too.  Yummmm, tacos.
    9592
     
    10299   print("Whohoo! Dinner's on you!")
    103100}}}
    104 
    105101''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    106102
    107103{{{
    108104   
    109  }}}
     105}}}
     106Yep, nothing was output. Nada.  In this case, we used variables which were numbers, and a greater-than expression to compare them.  Since the condition was ''False'', nothing was printed.
    110107
    111 Yep, nothing was output. Nada.  In this case, we used variables which were numbers, and a greater-than expression to compare them.  Since the condition was ''False'', nothing was printed.
    112 
    113 (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.)
    114  
    115 Now for a couple of other control structures that you should know...
     108(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...
    116109
    117110== For Loop ==
    118111The For loop control structure runs code repeats over a variable.  Try running this one:
     112
    119113{{{
    120114fruits = ['banana', 'apple', 'mango']
     
    123117print("That's alotta fruit!")
    124118}}}
    125 
    126119It should output:
    127120
     
    131124Current fruit : mango
    132125}}}
    133 
    134 This 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!"
     126This 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!"
    135127
    136128Note 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.
     
    138130For 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.
    139131
    140 
    141132== While Loop ==
    142 
    143 A 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. 
     133A 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.
    144134
    145135{{{
     
    152142print("Good bye!")
    153143}}}
    154 
    155144''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print
    156145
     
    158147
    159148}}}
     149Oh no, I'm tired, so I'm not going to tell you the answer for this one. You should be seeing a pattern by now.  You tell me---What should it print?    Count starts out at 0, and as long as count is less than 9 it will run the code in the block, then it will print "Good bye!".   What else does it print out?
    160150
    161 I'm tired, so Im not going to tell you the answer for this one. You should be seeing a pattern by now.  You tell me---What should it print?     
     151== Compound control statements ==
     152Lastly, you can combine all three of these in any sort combination, and nest them.  This is how you do real work in a program.   For example, try running this one:
     153
     154{{{
     155count=1
     156while (count<=7):
     157   if (count==4):
     158      print("more!")
     159   else:
     160      print(count, "potato,")
     161   count+=1
     162print("score!")
     163}}}
     164What does it output?   See how the indenting works when nesting?  Note that count+=1 is shorthand for count=count+1 because not typing those excess characters __could save your life__.
     165
     166Now, modify the code to make it repeat the word potato each time based on the current value of count.  For example:
     167
     168{{{
     1691 potato
     1702 potato potato
     1713 potato potato potato
     172more!
     1734 potato potato potato potato
     1745 potato potato potato potato potato
     1756 potato potato potato potato potato potato
     1767 potato potato potato potato potato potato potato
     177score!
     178}}}
     179Answer is [wiki:ProgrammingAnswerPotatoExercise here].
    162180
    163181----