Changes between Version 45 and Version 46 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 6:59:23 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v45 v46 53 53 If you want to do more in a program and actually make decisions in your code, we need to introduce control structures. 54 54 55 == If-Then-Else ==55 == If-Then-Else: Decide, already! == 56 56 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. You'll use these all the time. 57 57 … … 109 109 (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... 110 110 111 == For Loop ==111 == For Loop: Stop Hitting Yourself! == 112 112 The For loop control structure runs code repeats over a variable. Try running this one: 113 113 … … 132 132 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. 133 133 134 == While Loop ==134 == While Loop: One more time around the block, Alfred! == 135 135 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. 136 136 … … 183 183 Yeah, 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: 184 184 185 1. You can tell a print to not end with a new line by adding an end="" to the print statement, like this 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="")`''. '' 186 186 1. An empty print statement `print("")` just prints a new line, basically wrapping any line you're on. 187 187 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.]