Changes between Version 31 and Version 32 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 6:22:34 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v31 v32 44 44 Hello World! 45 45 }}} 46 The program told the computer to print the value of variable a on the screen. 46 The program told the computer to print the value of variable a on the screen. 47 47 48 48 And, boom, now you're a programmer. The rest of learning how to code is just more details. This is the end of Lesson 1. 49 49 50 51 = Lesson 2: Making decisions = 50 = Lesson 2: Making decisions = 52 51 In 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". 53 52 54 If you want to do more, we need to introduce control structures. 53 If you want to do more, we need to introduce control structures. 55 54 56 55 == If-Then-Else == … … 68 67 print("Hope you had a good time") 69 68 }}} 70 71 69 ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.'' It should print: 72 70 … … 80 78 Hope you had a good time 81 79 }}} 82 83 80 Three things to note about this program: 84 81 … … 87 84 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. 88 85 89 Now, in the editor replace the line with the if ''expression'' with 86 Now, in the editor replace the line with the if ''expression'' with 87 90 88 {{{ 91 89 if location == 'Poughkeepsie, NY': 92 }}} 93 90 }}} 94 91 and 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. 95 92 … … 102 99 print("Whohoo! Dinner's on you!") 103 100 }}} 104 105 101 ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.'' It should print: 106 102 107 103 {{{ 108 104 109 }}} 105 }}} 106 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. 110 107 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... 116 109 117 110 == For Loop == 118 111 The For loop control structure runs code repeats over a variable. Try running this one: 112 119 113 {{{ 120 114 fruits = ['banana', 'apple', 'mango'] … … 123 117 print("That's alotta fruit!") 124 118 }}} 125 126 119 It should output: 127 120 … … 131 124 Current fruit : mango 132 125 }}} 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!" 126 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!" 135 127 136 128 Note 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. … … 138 130 For 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. 139 131 140 141 132 == 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. 133 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. 144 134 145 135 {{{ … … 152 142 print("Good bye!") 153 143 }}} 154 155 144 ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.'' It should print 156 145 … … 158 147 159 148 }}} 149 Oh 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? 160 150 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 == 152 Lastly, 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 {{{ 155 count=1 156 while (count<=7): 157 if (count==4): 158 print("more!") 159 else: 160 print(count, "potato,") 161 count+=1 162 print("score!") 163 }}} 164 What 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 166 Now, modify the code to make it repeat the word potato each time based on the current value of count. For example: 167 168 {{{ 169 1 potato 170 2 potato potato 171 3 potato potato potato 172 more! 173 4 potato potato potato potato 174 5 potato potato potato potato potato 175 6 potato potato potato potato potato potato 176 7 potato potato potato potato potato potato potato 177 score! 178 }}} 179 Answer is [wiki:ProgrammingAnswerPotatoExercise here]. 162 180 163 181 ----