81 | | 1. The double equals in location=='HERE' on the first line. It is called an ''expression''. In this case it is an equals expression (other kinds of comparisons in python are listed here) . It returns ''True'' when the condition evaluates to ''True'', otherwise its ''False''. Note on that line the word 'HERE' is in quotes. That's to indicate its a string (a text value) not some variable (which doesn't exist). If you used ''='' instead of ''=='' it wouldn't work as you expected because it would be a variable assignment. If a IF-THEN-ELSE statement ever behaves in an unexpected way, check for this problem first, its a really common mistake. |
82 | | 1. Spacing and indents are important in Python. the line "We're here..." and the line below it are indented to indicate they are part of a statement ''block'' that runs when the condition evaluates to ''True''. The second block on the else is run when the condition to ''False'', and only contains one line. If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements. |
83 | | 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. [[Here|https://www.quackit.com/python/reference/python_3_escape_sequences.cfm]] are some other escape sequences you can use. |
| 82 | 1. The double equals in `location=='HERE'` on the first line. It is called an ''expression''. In this case it is an equals expression (other kinds of comparisons in python are listed here) . It returns ''True'' when the condition evaluates to ''True'', otherwise its ''False''. Note on that line the word 'HERE' is in quotes. That's to indicate its a string (a text value) not some variable (which doesn't exist). If you used `=` instead of `==` it wouldn't work as you expected because it would be a variable assignment. If a If-Then-Else statement ever behaves in an unexpected way, check for this problem first, its a really common mistake. |
| 83 | 1. Spacing and indents are important in Python. the line "We're here..." and the line below it are indented to indicate they are part of a statement ''block'' that runs when the condition evaluates to `True`. The second block on the else is run when the condition to `False`, and only contains one line. If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements. |
| 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. [[Here|https://www.quackit.com/python/reference/python_3_escape_sequences.cfm]] are some other escape sequences you can use. |
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. |
107 | | |
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... |
| 108 | |
| 109 | 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 | |
| 111 | (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 for yourself that the program doesn't see or run. The robotics team wants you to put comments in your code '''a lot'''--it makes the code easier for others 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... |
127 | | 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!" |
128 | | |
129 | | 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. |
130 | | |
131 | | 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. |
| 130 | |
| 131 | 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 better understand what's going on here, pretend that the For Loop is basically a big arrow. The first time the For loop runs, this arrow (called the ''index variable'') points 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 arrow to the second value and so on. After the third fruit, the for loop knows there's nothing left in fruits so it stops and the prints the last line "That's alotta fruit!" |
| 132 | |
| 133 | Note that the "Current fruit..." print statement is indented. Just like in the 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. |
| 134 | |
| 135 | For loops are handy if you want to avoid repeating yourself in code, and/or do the same thing over and over again. The less code you write, after all, the less chance you can make a mistake. Not to mention its easier to read. |
167 | | 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 extra characters __could save your life__. |
168 | | |
169 | | Now, modify the code to make it repeat the word potato each time based on the current value of count, and run it. When you're done, the output should look like: |
| 172 | |
| 173 | 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 extra characters __could save your life someday__. |
| 174 | |
| 175 | Now, your turn: modify the code to make it repeat the word potato each time based on the current value of count, and run it. When you're done, the output should look like: |