{"id":115,"date":"2021-05-22T16:18:08","date_gmt":"2021-05-22T16:18:08","guid":{"rendered":"http:\/\/juthon.com\/?p=115"},"modified":"2021-05-23T14:58:57","modified_gmt":"2021-05-23T14:58:57","slug":"passage-4-conditional-instruction","status":"publish","type":"post","link":"http:\/\/juthon.com\/index.php\/2021\/05\/22\/passage-4-conditional-instruction\/","title":{"rendered":"Passage 4: Conditional instruction"},"content":{"rendered":"\n<p>Very often when you write code, you want to perform different actions for different decisions. In&nbsp;programming this is sometimes called <em>variation<\/em>, but more often <em>decision making<\/em>, because it allows the execution of various code fragments only when a certain condition is met (when the user makes a certain decision).<\/p>\n\n\n\n<p>We will use conditional instructions to perform decision making.<\/p>\n\n\n\n<p><a href=\"#Odcinek-4\" data-type=\"internal\" data-id=\"#Odcinek-4\">Jump to Polish version<\/a><\/p>\n\n\n\n<p>The simplest example of decision making is the check box in the program: \"Continue? y \/ n\". The&nbsp;program only continues when the user presses the letter y.<\/p>\n\n\n\n<p>It is difficult to imagine modern programming without conditional instructions, which allow the user to <em>make choice<\/em>, and thus give him a real possibility of influencing the \"program flow\" (control flow). So\u2026 let's learn conditional statements!<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Introduction<\/h2>\n\n\n<p>If you've been following earlier passages of our series, you already know how to write a program that makes a short dialogue with the user: the program will ask the user for a name, remember it and reply with the greeting: Have a nice day, adding a name it remembered.<\/p>\n\n\n\n<p>Let's try to expand our program to further ask the user for age. And let's set ourselves an ambitious task. We vary the response of the program depending on the given&nbsp;age.<\/p>\n\n\n\n<p>Say you are building a program that \"gives a prize\" (whatever that prize might be) that is different for the older person and different for the younger person. If the user is 16 years old or older, the program generates the message: \"You are receiving the X award\". Otherwise (if the user is under 16), the program generates the message: \"You are receiving the Y&nbsp;award\".<\/p>\n\n\n\n<p>As you can see, we use the word if. The word if is a keyword in the Python programming language. In other words, a word that belongs to the syntax of the language and allows you to build the command structure. With the if keyword, we create a conditional statement in Python.<\/p>\n\n\n\n<p>Conditional statement \u2014 this is the topic of today's passage of our series. The conditional statement really isn't anything difficult. You will find out about it in a moment. In the future, you will use it hundreds and thousands of times without any problem.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">The if \u2014 else conditional<\/h2>\n\n\n<p>The if\u2014else conditional consists of a <em>condition<\/em> and a <em>command<\/em> that is executed when the condition fulfills, and a <em>second command<\/em> that is executed when the condition fails.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Conditions<\/h3>\n\n\n<p>A condition that \"fulfills\" is said to have a value: True.<\/p>\n\n\n\n<p>A condition that \"fails\" &nbsp;is said to have a value: &nbsp;False.<\/p>\n\n\n\n<p>We usually build conditions using equal (==), greater (&gt;), less (&lt;) operators. Python also defines not equal (! =), no-less (&gt; =), and no-greater (&lt;=) operators.<\/p>\n\n\n\n<p>An example of checking the truthfulness of the condition (based on a short program with the input command):<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print ('Enter number a:')<br>a=input()<br>print ('Enter number b:')<br>b=input()<br><br><\/p>\n\n\n<p>Let's evaluate the truthfulness of the condition <strong>a&gt;b<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns\">\n<div class=\"wp-block-column\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1015\" height=\"194\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia.png\" alt class=\"wp-image-119\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia.png 1015w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia-300x57.png 300w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia-768x147.png 768w\" sizes=\"(max-width: 1015px) 100vw, 1015px\"><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>Let's be honest: it is not complicated at all,&nbsp;right?<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Commands<\/h3>\n\n\n<p>Let's move on to the commands that are executed after the program checks the condition.<br>We have nothing special to say about these commands\u2026<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"61\" height=\"64\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Smiley.png\" alt class=\"wp-image-116\"><\/figure>\n\n\n\n<p>\u2026They can be any command you wish, be it actions (e.g. order to print a message on the screen) or calculations.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Conditional statement (i.e. condition + commands)<\/h3>\n\n\n<p>Now comes the time to combine the condition with the commands and show what the conditional statement looks&nbsp;like:<\/p>\n\n\n\n<p><strong>if condition is True:<br>&nbsp;&nbsp;&nbsp; statement 1<br>else:<br>&nbsp;&nbsp;&nbsp; statement 2<br><br><\/strong><strong><\/strong><\/p>\n\n\n\n<p>Translating into human language we read this: if condition is True, execute statement 1, otherwise, execute statement 2.<\/p>\n\n\n<h4 class=\"fonts-plugin-block \" style=\"font-weight: normal\">What should you pay attention to?<\/h4>\n\n\n<p>Note the obligatory colon at the end of the first line (the one with the condition) and after the else. Also pay attention to the so-called indentation. Commands following the colon are indented four spaces from the preceding line.<\/p>\n\n\n\n<p>Indentation is a very important part of Python syntax. They define the so-called code blocks. More about this in a moment.<\/p>\n\n\n\n<p>Let's write the first program using the conditional statement:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Enter number a:')<br>a=input()<br>print ('Enter number b:')<br>b=input()<br>if a&gt;b:<br>&nbsp;&nbsp;&nbsp; print ('The number a is greater.')<br>else:<br>&nbsp;&nbsp;&nbsp; print ('The number b is greater or the numbers are&nbsp;equal.')<br><\/p>\n\n\n<p>Conditional instructions are not difficult, the above program is almost intuitive to read, right? The&nbsp;program asks for two numbers: a and b \u2014 and remembers their value. Then we tell the program: if&nbsp;you find that the number a is greater than b, write \"The number a is greater\", otherwise (else) write \"The number b is greater or the numbers are&nbsp;equal\".<\/p>\n\n\n\n<p>OK. Maybe it's time to try to write something yourself? How about a little training?<\/p>\n\n\n\n<p><strong>Exercise 1<\/strong>. Try to write a short script that asks the user to enter two numbers (a and b). When these numbers are equal, the program will display the message: \"The numbers are equal\". Otherwise, the program will display the message: \"The numbers are not&nbsp;equal\".<\/p>\n\n\n\n<p><strong>Exercise 2<\/strong>. Write a short script asking the user to enter a color. The preferred color is white. Therefore, when the user enters the name \"white\", your program has to write \"Great, you guessed it!\" Otherwise, the program has to write: \"Not that&nbsp;color.\"<\/p>\n\n\n\n<p>You will find the answers to these two exercises at the end of the article.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Additional note:<\/h3>\n\n\n<p>The number of statements you put after a condition (after the line: \"if condition \u2026\") is by no means limited to one. If your program requires it, you can indicate two or three (or more) commands to be executed when the condition is true. The same goes for commands after the word else. For example, if&nbsp;you want to give four statements after \"if\" and three statements after the else, the schema of the conditional statement would look like&nbsp;this:<\/p>\n\n\n\n<p>if condition is&nbsp;True:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 2<\/p>\n\n\n\n<p>&nbsp;&nbsp; &nbsp;statement 3<\/p>\n\n\n\n<p>&nbsp;&nbsp; &nbsp;statement 4<\/p>\n\n\n\n<p>else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 5<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 6<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 7<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">We are expanding our program from previous passages<\/h3>\n\n\n<p>Let us remind you that we want to expand our program from previous episodes, which asked the user for a name and then greeted him with that name. Now the program will additionally ask the user for age and will react differently depending on the answer given:<\/p>\n\n\n\n<p>\u2022 if the user specifies the age of 16+, the program will generate the message: \"You are receiving the X&nbsp;award\".<\/p>\n\n\n\n<p>\u2022 otherwise, the program will generate the message: \"You are receiving the Y&nbsp;award\".<\/p>\n\n\n\n<p>In order to obtain the answer in two variants, we will of course use the if \u2014 else statement:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print ('Enter your name, please:')<br>a=input ()<br>print ('How old are you?')<br>age=int(input())<br>if age&gt;=16:<br>&nbsp;&nbsp;&nbsp; print ('You are receiving the X award.')<br>else:<br>&nbsp;&nbsp;&nbsp; print ('You are receiving the Y award.')<br>print ('Have a nice day,' +&nbsp;a&nbsp;+&nbsp;'.')<br><br><\/p>\n\n\n<p>Let's take a look at the script above. The first two lines ask for the first name and remember the user's answer. The answer is stored in the variable <em>a<\/em>. The next two lines are a request for age and (as before) \u2014 remembering the user's answer. The answer is stored in the <em>age<\/em> variable. Since we want the age to be stored as a number, we have wrapped the function input() with the function int(), which converts the text to a number (see Appendix to Passage 2).<\/p>\n\n\n\n<p>Next we implement the if \u2014 else conditional statement. The commands after \"if\" and after \"else\" are indented by four spaces. The last line of code refers to the variable <em>a<\/em> (which holds the name) and prints the <em>Have a nice day [name]<\/em> greeting.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Is the else necessary?<\/h3>\n\n\n<p>You may be wondering if the \"second part of the conditional\", that is, the piece of code that starts with \"else\", is always \u2014 in any case \u2014 needed. After all, we can wish that the program would react only to the user indicating the age of 16+ (awarding a prize), and otherwise it would simply not award a prize \u2014 in short, it would not do anything. In this case, we can use the shortened version of the if statement (without the else&nbsp;part).<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print ('Enter your name, please:')<br>a=input()<br>print ('How old are you?')<br>age=int(input())<br>if age&gt;=16:<br>&nbsp;&nbsp;&nbsp; print ('You are getting a prize.')<br>print ('Have a nice day,' +&nbsp;a&nbsp;+&nbsp;'.')<\/p>\n\n\n<p><br>If the user enters age 16+, the program will execute the snippet following <em>if<\/em> (a line indented by four spaces). Otherwise, the program will skip this piece of&nbsp;code.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Indentation<\/h3>\n\n\n<p>We promised to say a few more words about indentation in Python. Again, indenting is a very important part of Python syntax as it allows you to \"overmark\" blocks of code. Each programming language must contain (within its syntax) ways to define blocks of code. A code block is a set of commands that are logically or organizationally related to each other. The conditional if statement provides us with the first example to illustrate blocks of&nbsp;code.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print ('Enter number a: ')<br>a=input()<br>print ('Enter number b: ')<br>b=input ()<br>if a&gt; b:<br>&nbsp;&nbsp;&nbsp; print ('The number a is greater.')<br>&nbsp;&nbsp;&nbsp; print ('The number a is really bigger.')<br>&nbsp;&nbsp;&nbsp; print ('The number a is undoubtedly greater.')<br>print ('It's nice working with&nbsp;you.')<\/p>\n\n\n<p>As you can see, after the condition checking \"whether a is greater than b\", we gave three commands, differing in the content of the printed message. There is also a fourth command (\"It's nice working with you\"), but it is not indented by four spaces. What does it all mean? All in all, even without knowing Python's syntax, it's easy to guess. Lines that follow the line with \"condition\" and are indented by four spaces are subject to this condition. To be more precise: they are only executed when the condition is True. That's why we call them \"code block\"<a href=\"#_ftn1\">[1]<\/a>.<\/p>\n\n\n\n<p>The end of the block is marked out by the last indented line. The first unintended line means that we are already back in the main code (i.e. outside the conditional block), so the commands are executed regardless of whether the condition is met or&nbsp;not.<\/p>\n\n\n\n<p>Try it out with the script example above (run the code as a Python file). If you specify number a greater than number b (e.g. 4 and 3), the program will generate the output in the form of four&nbsp;lines:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">The number a is greater.<br>The number a is really bigger.<br>The number a is undoubtedly greater.<br>It's nice working with&nbsp;you.<\/p>\n\n\n<p>However, when you enter number a less than number b (such as 3 and 4), only one line will be printed:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">It's nice working with&nbsp;you.<\/p>\n\n\n<p>The command: <em>print ('It's nice working with you.') <\/em>\u2013 as \"unintended\" \u2013 is independent of the given condition and is executed in both variants.<\/p>\n\n\n\n<p>The use of indentation to mark blocks of code is a distinctive feature of Python. And proof of the genius of its creators. Indents provide great readability and clarity of the code. Most programming languages such as C, C ++, and Java use curly braces {} to define a block of code. And parentheses that are opened in different places and then closed in bulk in one (or vice versa) are what novice programmers hate the&nbsp;most.<\/p>\n\n\n\n<p>Believe us. Guido van Rossum, inventing indentations, saved you from the \"bracket thriller\".<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Finally something special \"for dessert\"<\/h2>\n\n\n<p>We decided to throw in a little topic additionally. In programming languages, the goal is to make the syntax as compact as possible. For this reason, various abbreviations are allowed. Experienced programmers praise them, beginners rather swear (until they get used to&nbsp;them).<\/p>\n\n\n\n<p>For example, it is well established to replace i=i+1 with i+=1<\/p>\n\n\n\n<p>Typically, you will often meet abbreviations that allow you to replace two lines of code with one line. We'll give you one of these abbreviations right now. As you may have noticed, input() statement is \u2014 almost always \u2014 preceded by a print() statement. Why? If you don't tell the user what to put into the program, how would they know? You have to indicate what the program is waiting for, whether to enter your name, date, or deposit amount, or any other data. Therefore, print and input must form an inseparable pair.<\/p>\n\n\n\n<p>The Python developers thought: if so, why not write the message, what we ask the user, right in the input() command? print() will then become redundant and we will save one line of&nbsp;code.<\/p>\n\n\n\n<p>Indeed, Python does allow this. In the input() command, you can put the so-called <em>prompt<\/em>, i.e. an incentive that tells the user what data we are waiting for.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Full form (traditional): <br>print('Enter your name:') <br>input()<\/td><td>Short form: <br>input('Enter your&nbsp;name:')&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Instead of a summary<\/h2>\n\n\n<p>In this passage, we discussed the if statement. Instead of a summary, we thought we would present a short post on the if statement by a 13-year-old Python enthusiast. In terms of simplicity and brevity, the&nbsp;post is unbeatable. If you had any doubts about the if statements, they will disappear after reading the post. Enjoy reading&nbsp;\u2026<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Let's get to know the if statement<\/h3>\n\n\n<p>We will use the \"if\" statement very often. We use the keyword <em>if<\/em> in it. The <em>if<\/em> keyword in Python means exactly the same as in ordinary \"human\" language, and means that if something is true, the program must execute the command, but if it is not true, it must not execute the command.<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">x = input ('Did you buy me milk as I asked? yes \/ no')<br>if x == 'yes':<br>&nbsp;&nbsp;&nbsp; print ('Thanks') # Don't forget to indent<br><br><\/p>\n\n\n<p>If we run this file, the output will look like&nbsp;this:<\/p>\n\n\n\n<p><em>Did you buy me milk as I asked? yes \/&nbsp;no<\/em><\/p>\n\n\n\n<p>We answer yes or&nbsp;no<\/p>\n\n\n\n<p>If we enter <em>yes<\/em>, the program will answer like this: <em>Thanks<\/em><\/p>\n\n\n\n<p>What if we type <em>no<\/em>? The program's response will&nbsp;be:<\/p>\n\n\n\n<p>*emptiness*<\/p>\n\n\n\n<p>So now we're going to improve our program to react to the second answer too!<\/p>\n\n\n<h4 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Else<\/h4>\n\n\n<p>The else keyword is supposed to perform the second option. As we said a moment ago, if we type <em>no<\/em> after running the script, the program won't respond, so now we'll improve it.<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">x = input ('Did you buy me milk as I asked? yes \/ no')<br>if x == 'yes': &nbsp;&nbsp;# don't forget the colon<br>&nbsp;&nbsp;&nbsp; print ('Thanks') # don't forget to indent<br>else:<br>&nbsp;&nbsp;&nbsp; print ('Why?') # We also indent<\/p>\n\n\n<p>If we run this file, the output will look like&nbsp;this:<\/p>\n\n\n\n<p><em>Did you buy me milk as I asked? yes \/&nbsp;no<\/em><\/p>\n\n\n\n<p>We answer <em>yes<\/em> or <em>no<\/em><\/p>\n\n\n\n<p>If we enter <em>yes<\/em>, the program will answer like this: <em>Thanks<\/em><\/p>\n\n\n\n<p>And if we enter <em>no<\/em>, the program will respond as follows: <em>Why?<\/em><\/p>\n\n\n\n<p>Thanks for reading the article and best regards.<\/p>\n\n\n\n<p><\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Answers for self-exercises<\/h2>\n\n\n<p>Exercise 1<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Enter first number: ')<br>a=input()<br>print('Enter second number: ')<br>b=input()<br>if a==b:<br>&nbsp;&nbsp;&nbsp; print('The numbers are equal.')<br>else:<br>&nbsp;&nbsp;&nbsp; print('The numbers are not&nbsp;equal.')<br><br><\/p>\n\n\n<p>Exercise 2<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Specify a colour, please: ')<br>x=input()<br>if x=='white':<br>&nbsp;&nbsp;&nbsp; print('Great, you guessed it!')<br>else:<br>&nbsp;&nbsp;&nbsp; print('This is not the&nbsp;color.')<br><br><\/p>\n\n\n<hr class=\"wp-block-separator\">\n\n\n\n<p><a href=\"#_ftnref1\">[1]<\/a> these commands are organizationally related, they are linked by dependence on one and the same condition<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"Odcinek-4\">Odcinek\u20114<\/p>\n\n\n<h1 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Odcinek 4: Instrukcje warunkowe<\/h1>\n\n\n<p>Witajcie.<\/p>\n\n\n\n<p>Dzi\u015b o wariantowaniu w programowaniu. Wariantowanie nazywa si\u0119 tak\u017ce \"decision making\", gdy\u017c umo\u017cliwia realizacj\u0119 r\u00f3\u017cnych fragment\u00f3w kodu tylko w\u00f3wczas, gdy zostanie spe\u0142niony okre\u015blony warunek (gdy u\u017cytkownik podejmie okre\u015blon\u0105 decyzj\u0119). Najprostszy przyk\u0142ad to pole wyboru w programie: \"Czy kontynuowa\u0107? T\/N\". Program kontynuuje dzia\u0142anie tylko w\u00f3wczas, je\u015bli u\u017cytkownik wci\u015bnie literk\u0119 T.<\/p>\n\n\n\n<p>Trudno sobie wyobrazi\u0107 nowoczesne programowanie bez <strong>instrukcji warunkowych<\/strong>, kt\u00f3re stwarzaj\u0105 mo\u017cliwo\u015b\u0107 wyboru po stronie u\u017cytkownika, a tym samym daj\u0105 mu realn\u0105 mo\u017cliwo\u015b\u0107 oddzia\u0142ywania na&nbsp;\"przep\u0142yw programu\" (control flow)<a href=\"#_ftn1\">[1]<\/a>. Zatem\u2026 uczymy si\u0119 instrukcji warunkowych!<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Wst\u0119p<\/h2>\n\n\n<p>Je\u015bli \u015bledzicie wcze\u015bniejsze odcinki naszego cyklu, wiecie ju\u017c, jak napisa\u0107 program, kt\u00f3ry prze\u00adprowadzi kr\u00f3tki dialog z u\u017cytkownikiem: poprosi go o podanie imienia, zapami\u0119ta je i odpowie pozdrowieniem: Mi\u0142ego dnia, dodaj\u0105c zapami\u0119tane imi\u0119.<\/p>\n\n\n\n<p>Spr\u00f3bujmy rozbudowa\u0107 nasz program, aby dalej zapyta\u0142 u\u017cytkownika o wiek. I postawmy sobie ambitne zadanie. <strong>Zr\u00f3\u017cnicujemy<\/strong> reakcj\u0119 programu zale\u017cnie od podanego wieku.<\/p>\n\n\n\n<p><em>Powiedzmy, \u017ce budujesz program, kt\u00f3ry przyznaje nagrod\u0119, inn\u0105 dla osoby starszej, inn\u0105 dla osoby m\u0142odszej.<\/em><\/p>\n\n\n\n<p><em>Je\u017celi u\u017cytkownik ma 16 lat lub wi\u0119cej, program generuje komunikat: \"Otrzymujesz nagrod\u0119 X\".<\/em><\/p>\n\n\n\n<p><em>W przeciwnym przypadku (je\u017celi u\u017cytkownik ma mniej ni\u017c 16 lat), program generuje komunikat: \"Otrzymujesz nagrod\u0119 Y\".<\/em><\/p>\n\n\n\n<p>Jak widzisz, pos\u0142ugujemy si\u0119 s\u0142owem <strong>je\u017celi<\/strong>. S\u0142owo <strong>je\u017celi (if)<\/strong> to s\u0142owo kluczowe w j\u0119zyku prog\u00adramowania Python. Inaczej m\u00f3wi\u0105c, s\u0142owo, kt\u00f3re nale\u017cy do sk\u0142adni j\u0119zyka i pozwala budowa\u0107 struktur\u0119 polece\u0144. Dzi\u0119ki s\u0142owu kluczowemu if utworzymy w Pythonie instrukcj\u0119 warunkow\u0105 (conditional statement).<\/p>\n\n\n\n<p><strong>Instrukcja warunkowa<\/strong> \u2013 to w\u0142a\u015bnie jest temat dzisiejszego odcinka naszego cyklu. Instrukcja warunkowa naprawd\u0119 nie jest niczym trudnym. Przekonasz si\u0119 o tym ju\u017c za chwil\u0119. W przysz\u0142o\u015bci b\u0119dziesz z niej korzysta\u0142 setki i tysi\u0105ce razy bez najmniejszego problemu.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Instrukcja warunkowa if\u2014else<\/h2>\n\n\n<p>Instrukcja warunkowa if\u2014else sk\u0142ada si\u0119 z <strong><em>warunku<\/em><\/strong> i <strong><em>polecenia<\/em><\/strong>, kt\u00f3re jest wykonywane gdy warunek spe\u0142nia si\u0119, oraz <strong><em>drugiego polecenia<\/em><\/strong>, kt\u00f3re jest wykonywane gdy warunek nie spe\u0142nia si\u0119.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Warunki<\/h3>\n\n\n<ul><li>O warunku, kt\u00f3ry \"spe\u0142nia si\u0119\" m\u00f3wimy, \u017ce posiada warto\u015b\u0107: Prawda (True).<\/li><li>O warunku, kt\u00f3ry \"nie spe\u0142nia si\u0119\" m\u00f3wimy, \u017ce posiada warto\u015b\u0107: Fa\u0142sz (False).<\/li><\/ul>\n\n\n\n<p>Warunki budujemy zazwyczaj przy pomocy operator\u00f3w r\u00f3wny (==), wi\u0119kszy (&gt;), mniejszy&nbsp;(&lt;).<\/p>\n\n\n\n<p>Python definiuje tak\u017ce operatory r\u00f3\u017cny (!=), nie-mniejszy (&gt;=) oraz nie-wi\u0119kszy&nbsp;(&lt;=).<\/p>\n\n\n\n<p>Przyk\u0142ad sprawdzania prawdziwo\u015bci warunku (na podstawie kr\u00f3tkiego programu z poleceniem input):<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj liczb\u0119 a: ')<br>a=input()<br>print('Podaj liczb\u0119 b: ')<br>b=input()<br><\/p>\n\n\n<p>Oce\u0144my prawdziwo\u015b\u0107 warunku &nbsp;<strong>a&gt;b<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1015\" height=\"194\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia.png\" alt class=\"wp-image-119\" srcset=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia.png 1015w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia-300x57.png 300w, http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Tabelcia-768x147.png 768w\" sizes=\"(max-width: 1015px) 100vw, 1015px\"><\/figure>\n\n\n\n<p>M\u00f3wi\u0105c szczerze: \u0142atwizna, prawda?<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Polecenia<\/h3>\n\n\n<p>Przejd\u017amy do polece\u0144, kt\u00f3re s\u0105 wykonywane po sprawdzeniu przez program warunku.<\/p>\n\n\n\n<p>Nie mamy nic specjalnego do powiedzenia na temat tych polece\u0144&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"61\" height=\"64\" src=\"http:\/\/juthon.com\/wp-content\/uploads\/2021\/05\/Smiley.png\" alt class=\"wp-image-116\"><\/figure>\n\n\n\n<p>Mog\u0105 to by\u0107 dowolne polecenia jakie sobie \u017cyczysz, czy to akcje (np. rozkaz wydrukowania komunikatu na ekranie), czy obliczenia.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Instrukcja warunkowa (czyli warunek + polecenia)<\/h3>\n\n\n<p>Czas po\u0142\u0105czy\u0107 warunek z poleceniami i pokaza\u0107, jak wygl\u0105da instrukcja warunkowa:<\/p>\n\n\n\n<p><strong>if condition is&nbsp;True:<\/strong><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; <strong>statement 1<\/strong><\/p>\n\n\n\n<p><strong>else:<\/strong><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;<strong> statement 2<\/strong><\/p>\n\n\n\n<p>Pos\u0142u\u017cyli\u015bmy si\u0119 schematem w j\u0119zyku angielskim, gdy\u017c jest on najbli\u017cszy sk\u0142adni Pythona. Przek\u0142adaj\u0105c na ludzki j\u0119zyk (human language) czytamy to: je\u017celi condition ma warto\u015b\u0107 True, wykonaj statement 1, w przeciwnym razie wykonaj statement 2<\/p>\n\n\n\n<p><em>Condition<\/em> to warunek, kt\u00f3rego prawdziwo\u015b\u0107 jest sprawdzana, <em>statement 1<\/em> i <em>statement 2<\/em> to polecenia.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Na co nale\u017cy zwr\u00f3ci\u0107 uwag\u0119?<\/h3>\n\n\n<p>Zwr\u00f3\u0107 uwag\u0119 na obligatoryjny dwukropek na ko\u0144cu pierwszej linii (tej z warunkiem) oraz po&nbsp;else.<\/p>\n\n\n\n<p>Zwr\u00f3\u0107 tak\u017ce uwag\u0119 na tzw. wci\u0119cia (indentation). Polecenia, u\u017cyte po dwukropku, s\u0105 wci\u0119te o cztery spacje w stosunku do linii poprzedzaj\u0105cej. Wci\u0119cia to bardzo wa\u017cny element sk\u0142adni Pythona. Wyznaczaj\u0105 one tzw. bloki kodu. Szerzej o tym za&nbsp;moment.<\/p>\n\n\n\n<p>Napiszmy pierwszy program z u\u017cyciem instrukcji warunkowej:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj liczb\u0119 a: ')<br>a=input()<br>print('Podaj liczb\u0119 b: ')<br>b=input()<br>if a&gt;b:<br>&nbsp;&nbsp;&nbsp; print('Liczba a jest wi\u0119ksza')<br>else:<br>&nbsp;&nbsp;&nbsp; print('Liczba b jest wi\u0119ksza albo liczby s\u0105&nbsp;r\u00f3wne')<\/p>\n\n\n<p>Instrukcje warunkowe nie s\u0105 trudne, powy\u017cszy program czyta si\u0119 niemal intuicyjnie, prawda?<br>Program prosi o podanie dw\u00f3ch liczb: a i b \u2013 i zapami\u0119tuje ich warto\u015b\u0107. Nast\u0119pnie nakazujemy programowi: je\u015bli stwierdzisz (if), \u017ce liczba a jest wi\u0119ksza od b, napisz: \"Liczba a jest wi\u0119ksza\", w&nbsp;przeciwnym razie (else) napisz \"Liczba b jest wi\u0119ksza albo liczby s\u0105&nbsp;r\u00f3wne\".<\/p>\n\n\n\n<p>OK. Mo\u017ce czas spr\u00f3bowa\u0107 napisa\u0107 co\u015b samodzielnie? Co powiesz na ma\u0142y trening?<\/p>\n\n\n\n<p><strong>\u0106wiczenie 1<\/strong>. Spr\u00f3buj samodzielnie napisa\u0107 kr\u00f3tki programik, kt\u00f3ry prosi u\u017cytkownika o podanie dw\u00f3ch liczb (a i b). Gdy liczby te s\u0105 r\u00f3wne, program b\u0119dzie wy\u015bwietla\u0142 komunikat: \"Liczby s\u0105 r\u00f3wne\". W przeciwnym razie program wy\u015bwietli komunikat: \"Liczby nie s\u0105&nbsp;r\u00f3wne\".<\/p>\n\n\n\n<p><strong>\u0106wiczenie 2<\/strong>. Napisz kr\u00f3tki programik, kt\u00f3ry prosi u\u017cytkownika o podanie koloru. Premiowanym kolorem jest bia\u0142y. Dlatego gdy u\u017cytkownik poda nazw\u0119 \"bia\u0142y\", tw\u00f3j program ma w odpowiedzi napisa\u0107 \"\u015awietnie, zgad\u0142e\u015b!\". W przeciwnym razie program ma napisa\u0107: \"Nie ten&nbsp;kolor.\"<\/p>\n\n\n\n<p>Odpowiedzi znajdziesz na ko\u0144cu artyku\u0142u.<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Dodatkowa uwaga:<\/h3>\n\n\n<p>Ilo\u015b\u0107 polece\u0144, jakie umieszczasz po warunku (if condition\u2026) nie jest bynajmniej ograniczona do jednego. Je\u015bli&nbsp;tego wymaga tw\u00f3j program, mo\u017cesz z powodzeniem wskaza\u0107 dwa lub trzy (lub wi\u0119cej) polece\u0144 wykonywanych, gdy warunek zostanie spe\u0142niony. To samo dotyczy polece\u0144 po s\u0142owie else. Przyk\u0142adowo gdy chcesz da\u0107 cztery polecenia po \"if\" i trzy polecenia po else, schemat instrukcji warunkowej wygl\u0105da\u0142 b\u0119dzie tak:<\/p>\n\n\n\n<p>if condition is&nbsp;True:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 2<\/p>\n\n\n\n<p>&nbsp;&nbsp; &nbsp;statement 3<\/p>\n\n\n\n<p>&nbsp;&nbsp; &nbsp;statement 4<\/p>\n\n\n\n<p>else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 5<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 6<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; statement 7<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Rozbudowujemy nasz program z poprzednich odcink\u00f3w<\/h2>\n\n\n<p>Przypomnijmy, \u017ce nasz program z poprzednich odcink\u00f3w, kt\u00f3ry prosi\u0142 u\u017cytkownika o podanie imienia, a nast\u0119pnie pozdrawia\u0142 go tym imieniem, chcemy obecnie rozbudowa\u0107. Program prosi\u0142 b\u0119dzie u\u017cytkownika o podanie wieku i reagowa\u0142 w r\u00f3\u017cny spos\u00f3b, zale\u017cnie od udzielonej odpowiedzi:<\/p>\n\n\n\n<ul><li>je\u017celi u\u017cytkownik poda wiek 16+, program wygeneruje komunikat: \"Otrzymujesz nagrod\u0119 X\".<\/li><li>w przeciwnym razie program wygeneruje komunikat: \" Otrzymujesz nagrod\u0119 Y\".<\/li><\/ul>\n\n\n\n<p>W celu uzyskania odpowiedzi w dw\u00f3ch wariantach, pos\u0142u\u017cymy si\u0119 oczywi\u015bcie instrukcj\u0105 if\u2014else<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj twoje imi\u0119, prosz\u0119:')<br>a = input()<br>print('Ile masz lat?')<br>age = int(input())<br>if age&gt;=16:<br>&nbsp;&nbsp;&nbsp; print('Otrzymujesz nagrod\u0119 X.')<br>else:<br>&nbsp;&nbsp;&nbsp; print('Otrzymujesz nagrod\u0119 Y.')<br>print('Mi\u0142ego dnia, '+a+'.')<br><\/p>\n\n\n<p>Przyjrzyjmy si\u0119 powy\u017cszemu skryptowi. Pierwsze dwie linie to pro\u015bba o podanie imienia oraz zapami\u0119tanie odpowiedzi u\u017cytkownika. Odpowied\u017a zostaje zapami\u0119tana w zmiennej <em>a<\/em>.<\/p>\n\n\n\n<p>Kolejne dwie linie to pro\u015bba o podanie wieku i (jak poprzednio) \u2013 zapami\u0119tanie odpowiedzi u\u017cytkownika. Odpowied\u017a zostaje zapami\u0119tana w zmiennej <em>age<\/em>. &nbsp;Poniewa\u017c chcemy, aby wiek zapami\u0119tany by\u0142 jako liczba, funkcj\u0119 input() opakowali\u015bmy dodatkowo funkcj\u0105 int(), kt\u00f3ra konwertuje tekst na liczb\u0119 (por\u00f3wnaj Dodatek do odcinka 2).<\/p>\n\n\n\n<p>Dalej wdra\u017camy instrukcj\u0119 warunkow\u0105 if\u2014else. Polecenia po \"if\" i po \"else\" wci\u0119te s\u0105 o cztery spacje<a href=\"#_ftn2\">[2]<\/a>.<\/p>\n\n\n\n<p>Ostatnia linia kodu odwo\u0142uje si\u0119 do zmiennej <em>a<\/em> i drukuje pozdrowienie Mi\u0142ego dnia [imi\u0119].<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Czy else jest koniecznie potrzebne?<\/h2>\n\n\n<p>By\u0107 mo\u017ce zastanawiasz si\u0119, czy \"druga cz\u0119\u015b\u0107 instrukcji warunkowej\", czyli fragment kodu zaczynaj\u0105cy si\u0119 od \"else\", jest zawsze \u2013 w ka\u017cdej sytuacji \u2013 potrzebny.<\/p>\n\n\n\n<p>Mo\u017cemy przecie\u017c \u017cyczy\u0107 sobie, aby program reagowa\u0142 jedynie na podanie przez u\u017cytkownika wieku 16+ (przyznawa\u0142 nagrod\u0119), za\u015b w przeciwnej sytuacji po prostu nie przyznawa\u0142 nagrody \u2013 czyli, m\u00f3wi\u0105c kr\u00f3tko: nie robi\u0142&nbsp;nic.<\/p>\n\n\n\n<p>W takiej sytuacji mo\u017cemy pos\u0142ugiwa\u0107 si\u0119 skr\u00f3con\u0105 wersj\u0105 instrukcji if (bez cz\u0119\u015bci zwi\u0105zanej z&nbsp;else).<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj twoje imi\u0119, prosz\u0119:')<br>a = input()<br>print('Ile masz lat?')<br>age = int(input())<br>if age&gt;=16:<br>&nbsp;&nbsp;&nbsp; print('Otrzymujesz nagrod\u0119.')<br>print('Mi\u0142ego dnia, '+a+'.')<\/p>\n\n\n<p>Je\u017celi u\u017cytkownik wpisze wiek 16+, program wykona fragment kodu nast\u0119puj\u0105cy po if (linia wci\u0119ta o&nbsp;cztery spacje). W przeciwnym razie program <strong>pominie<\/strong> ten fragment kodu.<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Indentation czyli wci\u0119cia<\/h2>\n\n\n<p>Obiecali\u015bmy powiedzie\u0107 kilka s\u0142\u00f3w wi\u0119cej na temat wci\u0119\u0107 (indentation) w Pythonie.<\/p>\n\n\n\n<p>Powt\u00f3rzmy: wci\u0119cia to bardzo wa\u017cny element sk\u0142adni Pythona, gdy\u017c pozwalaj\u0105 \"wyodr\u0119bni\u0107\" bloki kodu. Ka\u017cdy j\u0119zyk programowania musi zawiera\u0107 (w ramach swojej sk\u0142adni) sposoby na definiowanie blok\u00f3w kodu. <strong>Blok kodu<\/strong> to zestaw polece\u0144 logicznie lub organizacyjnie ze sob\u0105 powi\u0105zanych.<\/p>\n\n\n\n<p>Instrukcja warunkowa if dostarcza nam pierwszego przyk\u0142adu na zilustrowanie blok\u00f3w kodu.<br><br><\/p>\n\n\n\n<p>Przyk\u0142ad<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Podaj liczb\u0119 a: ')<br>a=input()<br>print('Podaj liczb\u0119 b: ')<br>b=input()<br>if a&gt;b:<br>&nbsp;&nbsp;&nbsp; print('Liczba a jest wi\u0119ksza.')<br>&nbsp;&nbsp;&nbsp; print('Liczba a jest naprawd\u0119 wi\u0119ksza.')<br>&nbsp;&nbsp;&nbsp; print('Liczba a jest bez w\u0105tpienia wi\u0119ksza.')<br>print('Mi\u0142o si\u0119 z Tob\u0105 pracuje.')<br><\/p>\n\n\n<p>Jak wida\u0107, po warunku sprawdzaj\u0105cym \"czy a jest wi\u0119ksze od b\", dali\u015bmy <strong>trzy<\/strong> polecenia, r\u00f3\u017cni\u0105ce si\u0119 tre\u015bci\u0105 drukowanego komunikatu. Jest tak\u017ce czwarte polecenie (o tre\u015bci: \"Mi\u0142o si\u0119 z Tob\u0105 pracuje\"), ale nie jest ono wci\u0119te o cztery spacje.<\/p>\n\n\n\n<p>Co to wszystko oznacza? W sumie, nawet nie znaj\u0105c sk\u0142adni Pythona, mo\u017cna \u0142atwo si\u0119 domy\u015ble\u0107. Wiersze, kt\u00f3re nast\u0119puj\u0105 po wierszu z \"warunkiem\" (condition) i s\u0105 wci\u0119te o cztery spacje, s\u0105 niejako podporz\u0105dkowane temu warunkowi. Wyra\u017caj\u0105c si\u0119 precyzyjniej: s\u0105 wykonywane tylko w\u00f3wczas, gdy warunek przyjmuje warto\u015b\u0107 True. Dlatego nazywamy je \"blokiem kodu\"<a href=\"#_ftn3\">[3]<\/a>.<\/p>\n\n\n\n<p>Koniec bloku kodu wyznacza ostatnia wci\u0119ta linia. Pierwsza \u2013 z nast\u0119puj\u0105cych dalej \u2013 niewci\u0119ta linia, oznacza, \u017ce jeste\u015bmy ju\u017c z powrotem w kodzie g\u0142\u00f3wnym (czyli poza blokiem warunkowym), a zatem polecenia wykonywane s\u0105 bez wzgl\u0119du na spe\u0142nienie czy niespe\u0142nienie warunku.<\/p>\n\n\n\n<p>Wypr\u00f3buj to na podanym wy\u017cej przyk\u0142adzie skryptu (uruchom kod jako plik Pythona). Je\u015bli podasz liczb\u0119 <em>a<\/em> wi\u0119ksz\u0105 od liczby <em>b<\/em> (np. 4 i 3), program wygeneruje output w postaci czterech linii:<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Liczba a jest wi\u0119ksza.<br>Liczba a jest naprawd\u0119 wi\u0119ksza.<br>Liczba a jest bez w\u0105tpienia wi\u0119ksza.<br>Mi\u0142o si\u0119 z Tob\u0105 pracuje.<\/p>\n\n\n<p>Gdy jednak podasz liczb\u0119 <em>a<\/em> mniejsz\u0105 od <em>b<\/em> (np. 3 i 4), wydrukowana zostanie tylko jedna&nbsp;linia.<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">Mi\u0142o si\u0119 z Tob\u0105 pracuje.<\/p>\n\n\n<p>Polecenie: print('Mi\u0142o si\u0119 z Tob\u0105 pracuje.') jako \"niewci\u0119te\" jest niezale\u017cne od podanego warunku i wykonywane w obydwu wariantach.<\/p>\n\n\n\n<p>Stosowanie wci\u0119\u0107 dla oznaczenia blok\u00f3w kodu to charakterystyczny wyr\u00f3\u017cnik j\u0119zyka Python.<br>I dow\u00f3d geniuszu jego tw\u00f3rc\u00f3w. Wci\u0119cia daj\u0105 bowiem ogromn\u0105 czytelno\u015b\u0107 i przejrzysto\u015b\u0107 kodu.<\/p>\n\n\n\n<p>Wi\u0119kszo\u015b\u0107 j\u0119zyk\u00f3w programowania, takich jak C, C++ i Java, u\u017cywa nawias\u00f3w klamrowych {} do&nbsp;definiowania bloku kodu. A nawiasy, otwierane w r\u00f3\u017cnych miejscach a potem hurtowo zamykane w jednym (albo na odwr\u00f3t) s\u0105 tym, czego pocz\u0105tkuj\u0105cy programi\u015bci nienawidz\u0105 najbardziej. Uwierz nam. Guido van Rossum, wymy\u015blaj\u0105c wci\u0119cia, uratowa\u0142 Ci\u0119 od \"nawiasowego thrillera\".<\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Jeszcze co\u015b specjalnego \"na&nbsp;deser\"<\/h2>\n\n\n<p>Postanowili\u015bmy \u2013 niejako dodatkowo \u2013 wrzuci\u0107 pewien ma\u0142y temacik. W j\u0119zykach programowania d\u0105\u017cy si\u0119 do tego, aby sk\u0142adnia by\u0142a mo\u017cliwie zwarta, kr\u00f3tka i nierozwlek\u0142a. Z tego powodu dopuszcza si\u0119 rozmaite skr\u00f3ty. Wytrawni programi\u015bci chwal\u0105 je sobie, pocz\u0105tkuj\u0105cy raczej przeklinaj\u0105 (dop\u00f3ki si\u0119 z nimi nie oswoj\u0105).<\/p>\n\n\n\n<p>Przyk\u0142adowo, dobrze ugruntowane jest zast\u0119powanie zapisu i=i+1 zapisem i+=1<\/p>\n\n\n\n<p>Cz\u0119sto spotyka si\u0119 skr\u00f3ty, pozwalaj\u0105ce zast\u0105pi\u0107 dwie linie kodu jedn\u0105 lini\u0105. Jeden z takich skr\u00f3t\u00f3w podamy w tej chwili. Zauwa\u017cy\u0142e\u015b pewnie, \u017ce polecenie input() jest \u2013 niemal zawsze \u2013 poprzedzane poleceniem print(). Dlaczego? Je\u015bli nie napiszesz u\u017cytkownikowi, co ma wprowadzi\u0107 do programu, sk\u0105d mia\u0142by to wiedzie\u0107? Musisz zasygnalizowa\u0107, na co czeka program, czy na podanie imienia, czy daty, czy kwoty wp\u0142aty, czy jakichkolwiek innych danych. Dlatego print i input musz\u0105 tworzy\u0107 nieroz\u0142\u0105czn\u0105 park\u0119.<\/p>\n\n\n\n<p>Tw\u00f3rcy Pythona pomy\u015bleli sobie: skoro tak, czemu nie zapisa\u0107 komunikatu, o co prosimy u\u017cytkownika, od razu w poleceniu input()? print() stanie si\u0119 w\u00f3wczas zb\u0119dne i zaoszcz\u0119dzimy jedn\u0105 linijk\u0119 kodu.<\/p>\n\n\n\n<p>I rzeczywi\u015bcie, Python dopuszcza tak\u0105 mo\u017cliwo\u015b\u0107. W poleceniu input() umie\u015bci\u0107 mo\u017cesz tzw. prompt, czyli zach\u0119t\u0119, komunikuj\u0105c\u0105 u\u017cytkownikowi, na podanie jakich danych czekamy.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Pe\u0142na forma (tradycyjna): <br>print('Podaj twoje imi\u0119: ') <br>input() &nbsp;<\/td><td>Forma skr\u00f3cona: <br>input('Podaj twoje&nbsp;imi\u0119:&nbsp;')<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Zamiast podsumowania<\/h2>\n\n\n<p>W niniejszym odcinku zapoznali\u015bmy si\u0119 z instrukcj\u0105 warunkow\u0105 if. Pomy\u015bleli\u015bmy, \u017ce zamiast podsumowania, zaprezentujemy kr\u00f3tki post, omawiaj\u0105cy instrukcj\u0119 warunkow\u0105 if, napisany przez trzynastoletniego mi\u0142o\u015bnika Pythona. Pod wzgl\u0119dem prostoty i zwi\u0119z\u0142o\u015bci, post jest bezkonkurencyjny. Je\u015bli mia\u0142e\u015b jeszcze jakie\u015b w\u0105tpliwo\u015bci w zakresie instrukcji if, po jego przeczytaniu na pewno znikn\u0105. Mi\u0142ej lektury\u2026<\/p>\n\n\n<h3 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Poznajmy instrukcj\u0119 if<\/h3>\n\n\n<p>Instrukcji \u201eif\u201d b\u0119dziemy u\u017cywa\u0107 bardzo cz\u0119sto. Wykorzystujemy w niej s\u0142owo kluczowe: w\u0142a\u015bnie if.<\/p>\n\n\n\n<p>S\u0142owo kluczowe if w Pythonie znaczy dok\u0142adnie to samo, co w zwyk\u0142ym \"ludzkim\" j\u0119zyku i oznacza, \u017ce je\u017celi co\u015b jest prawd\u0105, program ma wykona\u0107 polecenie, jednak je\u017celi nie jest prawd\u0105, ma nie wykonywa\u0107 polecenia.<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">x = input('Czy kupi\u0142e\u015b mi mleko jak prosi\u0142am? tak\/nie&nbsp; ')<br>if x == 'tak':<br>&nbsp;&nbsp;&nbsp; print('Dzi\u0119ki') # Nie zapominamy o wci\u0119ciu<br><br><\/p>\n\n\n<p>Je\u017celi uruchomimy ten plik, output b\u0119dzie wygl\u0105da\u0107 tak:<\/p>\n\n\n\n<p><em>Czy kupi\u0142e\u015b mi mleko jak prosi\u0142am? tak\/nie<\/em><\/p>\n\n\n\n<p>Odpowiadamy <em>tak<\/em> lub <em>nie<\/em><\/p>\n\n\n\n<p>Je\u015bli wpiszemy <em>tak <\/em>odpowied\u017a programu b\u0119dzie wygl\u0105da\u0107:<\/p>\n\n\n\n<p><em>Dzi\u0119ki<\/em><\/p>\n\n\n\n<p>A co je\u015bli wpiszemy <em>nie<\/em> ? odpowied\u017a programu b\u0119dzie wygl\u0105da\u0107:<\/p>\n\n\n\n<p>*pustka*<\/p>\n\n\n\n<p>Wi\u0119c teraz ulepszymy nasz program \u017ceby reagowa\u0142 te\u017c na drug\u0105 odpowied\u017a!<\/p>\n\n\n<h4 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Else<\/h4>\n\n\n<p>S\u0142owo kluczowe else ma wykonywa\u0107 drug\u0105&nbsp;opcj\u0119.<\/p>\n\n\n\n<p>Jak przed chwil\u0105 m\u00f3wili\u015bmy, je\u017celi po uruchomieniu skryptu wpiszemy <em>nie<\/em> \u2013 &nbsp;program nie odpowie nic, wi\u0119c teraz go ulepszymy.<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">x = input('Czy kupi\u0142e\u015b mi mleko jak prosi\u0142am? tak\/nie&nbsp; ')<br>if x == 'tak':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;# nie zapominamy o dwukropku<br>&nbsp;&nbsp;&nbsp; print('Dzi\u0119ki') &nbsp;&nbsp;# nie zapominamy o wci\u0119ciu<br>else:<br>&nbsp;&nbsp;&nbsp; print('Dlaczego?')&nbsp;&nbsp;&nbsp; # R\u00f3wnie\u017c dajemy wci\u0119cie<br><br><\/p>\n\n\n<p>Je\u017celi uruchomimy ten plik, output b\u0119dzie wygl\u0105da\u0107 tak:<\/p>\n\n\n\n<p><em>Czy kupi\u0142e\u015b mi mleko jak prosi\u0142am? tak\/nie<\/em><\/p>\n\n\n\n<p>Odpowiadamy <em>tak<\/em> lub <em>nie<\/em><\/p>\n\n\n\n<p>Je\u015bli wpiszemy <em>tak<\/em> odpowied\u017a programu b\u0119dzie wygl\u0105da\u0107:<\/p>\n\n\n\n<p><em>Dzi\u0119ki<\/em><\/p>\n\n\n\n<p>A je\u017celi wpiszemy <em>nie<\/em> odpowied\u017a programu b\u0119dzie wygl\u0105da\u0107:<\/p>\n\n\n\n<p><em>Dlaczego?<\/em><\/p>\n\n\n\n<p>Dzi\u0119kuj\u0119 za przeczytanie artyku\u0142u i pozdrawiam.<\/p>\n\n\n\n<p><\/p>\n\n\n<h2 class=\"fonts-plugin-block \" style=\"font-weight: normal\">Odpowiedzi do samodzielnych \u0107wicze\u0144<\/h2>\n\n\n<p>\u0106wiczenie 1<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Enter first number: ')<br>a=input()<br>print('Enter second number: ')<br>b=input()<br>if a==b:<br>&nbsp;&nbsp;&nbsp; print('The numbers are equal.')<br>else:<br>&nbsp;&nbsp;&nbsp; print('The numbers are not&nbsp;equal.')<br><br><\/p>\n\n\n<p>\u0106wiczenie 2<\/p>\n\n\n<p class=\"fonts-plugin-block \" style=\"font-family: consolas;font-weight: normal\">print('Specify a color, please: ')<br>x=input()<br>if x=='white':<br>&nbsp;&nbsp;&nbsp; print('Great, you guessed it!')<br>else:<br>&nbsp;&nbsp;&nbsp; print('This is not the&nbsp;color.')<br><br><\/p>\n\n\n<hr class=\"wp-block-separator\">\n\n\n\n<p><a href=\"#_ftnref1\">[1]<\/a> Control flow \u2013 terminem tym okre\u015bla si\u0119 kolejno\u015b\u0107 wykonywania polece\u0144 w programie<\/p>\n\n\n\n<p><a href=\"#_ftnref2\">[2]<\/a> ale ostatnia linijka skryptu nie jest wci\u0119ta; dlaczego? do tematu \"wci\u0119\u0107\" za chwil\u0119 wr\u00f3cimy<\/p>\n\n\n\n<p><a href=\"#_ftnref3\">[3]<\/a> polecenia te s\u0105 organizacyjnie powi\u0105zane, wi\u0105\u017ce je zale\u017cno\u015b\u0107 od jednego i tego samego warunku<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Very often when you write code, you want to perform different actions for different decisions.&nbsp;[\u2026]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wp_typography_post_enhancements_disabled":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/115"}],"collection":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":4,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":123,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/posts\/115\/revisions\/123"}],"wp:attachment":[{"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juthon.com\/index.php\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}