|
10 | 10 |
|
11 | 11 | {% block main %}
|
12 | 12 | <div class="row pt-5">
|
13 |
| - <div class="col-12"> |
14 |
| - <h2>Contents for lesson here!</h2> |
15 | 13 |
|
16 |
| - <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p> |
17 |
| - <p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</p> |
| 14 | + <div class="col-12 py-3"> |
| 15 | + <h2>Conditionals</h2> |
| 16 | + |
| 17 | + <div class="lesson-text py-3"> |
| 18 | + <p>Conditionals allow programs to change their behavior based on whether some statement is true or false. Let's try this out by writing a script that will give different outputs based on the weather:</p> |
| 19 | + |
| 20 | + <blockquote> |
| 21 | + <code> |
| 22 | + <p>weather = "sunny"</p> |
| 23 | + |
| 24 | + <p>if weather == "sunny":</p> |
| 25 | + <p> print("Bring your shades")</p> |
| 26 | + <p>else:</p> |
| 27 | + <p> print("I don't know what you should bring! I'm just a little program...")</p> |
| 28 | + </code> |
| 29 | + </blockquote> |
| 30 | + |
| 31 | + <p>In our first line, we set a variable <kbd>weather</kbd> to the string "sunny," representing what the weather is like outside. The <kbd>if</kbd> statement checks whether the variable weather is set to "sunny." If it is, the code in the block beneath is executed, so the text "Bring your shades" will be printed.</p> |
| 32 | + |
| 33 | + <p>The <kbd>else</kbd> statement handles any inputs that aren't "sunny"—the program merely prints out that it doesn't know what you should bring. Try this script out both with the variable set to "sunny" and the variable set to some other value.</p> |
| 34 | + |
| 35 | + <p>What if we want our program to handle other kinds of weather, giving different messages for each one? Other cases after the first <kbd>if</kbd> statement are handled with <kbd>elif</kbd>:</p> |
| 36 | + |
| 37 | + <blockquote> |
| 38 | + <code> |
| 39 | + <p>weather = "sunny"</p> |
| 40 | + |
| 41 | + <p>if weather == "sunny":</p> |
| 42 | + <p> print("Bring your shades") |
| 43 | + <p>elif weather == "rainy":</p> |
| 44 | + <p> print("Bring your umbrella") |
| 45 | + <p>elif weather == "snowy":</p> |
| 46 | + <p> print("Bring your wooly muffler") |
| 47 | + <p>else:</p> |
| 48 | + <p> print("I don't know what you should bring! I'm just a little program...")</p> |
| 49 | + </code> |
| 50 | + </blockquote> |
| 51 | + |
| 52 | + <p>You can add as many <kbd>elif</kbd> statements as you need, meaning that conditionals in Python have one <kbd>if</kbd> statement, any number of <kbd>elif</kbd> statements, and one <kbd>else</kbd> statement that catches any input not covered by <kbd>if</kbd> or <kbd>elif</kbd>. Over the next sections, we'll work on improving this little application, making it able to handle user input directly.</p> |
| 53 | + |
| 54 | + <h3 class="pt-4">Challenge</h3> |
| 55 | + |
| 56 | + <p>Add two more <kbd>elif</kbd> statements to this program to make it better able to handle different kinds of weather.</p> |
| 57 | + </div> |
18 | 58 | </div>
|
| 59 | + |
19 | 60 | </div>
|
20 | 61 |
|
21 | 62 | {% include 'lesson/lesson-elements/pagination.html' %}
|
| 63 | + <div class="row pt-5"> |
| 64 | + |
| 65 | + <div class="col-12 py-3"> |
| 66 | + <h4 class="small uppercase">Workshop overall progress</h4> |
| 67 | + <div class="progress"> |
| 68 | + <div class="progress-bar progress-bar-striped" role="progressbar" style="width: 10%" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100"></div> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </div> |
22 | 72 |
|
23 | 73 | {% endblock %}
|
0 commit comments