{"id":5559,"date":"2012-09-13T16:33:17","date_gmt":"2012-09-13T20:33:17","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=5559"},"modified":"2024-05-23T03:57:00","modified_gmt":"2024-05-23T07:57:00","slug":"6-de","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/6-de\/","title":{"rendered":"6: If"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p>Bis jetzt haben alle unsere Programme ihre Anweisungen der Reihe nach ausgef\u00fchrt, ohne irgendwelche Zeilen zu \u00fcberspringen. Hier ist ein Beispiel; Schritt vor und zur\u00fcck durch den Code mit den Kn\u00f6pfen oder den Pfeiltasten auf der Tastatur.<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=birthYear+%3D+1982%0AcurrentYear+%3D+2013%0Aage+%3D+currentYear+-+birthYear%0Aprint%28%27You+are%27%2C+age%2C+%27years+old%27%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p>Die neue M\u00f6glichkeit in dieser Lektion ist die <strong>if-Anweisung<\/strong>, die uns erlaubt, Aktionen nur durchzuf\u00fchren, wenn bestimmte Bedingungen zutreffen, und ansonsten Zeilen zu \u00fcberspringen. \u00c4ndern wir zum Beispiel unser Programm und geben wir eine extra Nachricht aus, wenn das Alter von einem gro\u00df genug ist. Wir werden zwei Zeilen hinzuf\u00fcgen: <code>if age &gt; 65:<\/code> (bedeutet, \"falls <code>age<\/code> gr\u00f6\u00dfer als 65\") und <code>  print('Genie\u00dfe die Rente!')<\/code>. Jetzt schrittweise durch den Code:<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=birthYear+%3D+1982%0AcurrentYear+%3D+2013%0Aage+%3D+currentYear+-+birthYear%0Aif+age+%3E+65%3A%0A+print%28%27Genie%C3%9Fe+die+Rente%21%27%29%0Aprint%28%27Du+bist%27%2C+age%2C+%27Jahre+alt%27%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p>Beachte, dass die neue print-Aussage <strong>\u00fcbersprungen<\/strong> wurde,weil <code>age<\/code> nur 31 war! Was passiert mit <strong>genau dem selben Programm<\/strong>, wenn <code>birthYear<\/code> auf 1928 ge\u00e4ndert wird?<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=birthYear+%3D+1928%0AcurrentYear+%3D+2013%0Aage+%3D+currentYear+-+birthYear%0Aif+age+%3E+65%3A%0A+print%28%27Genie%C3%9Fe+die+Rente%21%27%29%0Aprint%28%27Du+bist%27%2C+age%2C+%27Jahre+alt%27%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p>Siehst du den Unterschied? Die untere Version (mit <code>age<\/code> <code><\/code>85) gab zwei Zeilen aus und \u00fcbersprang keine. Dies ist genau die Absicht der <strong>if-Anweisung<\/strong>: um etwas zu \u00fcberpr\u00fcfen (ist <code>age<\/code> gr\u00f6\u00dfer als 65?) und um bestimmte Aktionen durchzuf\u00fchren (Ausgabe von <code>Genie\u00dfe die Rente!<\/code>), nur wenn die \u00dcberpr\u00fcfung als richtig gilt.<\/p>\n<p>Wir nennen die Sache, die \u00fcberpr\u00fcft wird, <strong>condition<\/strong>, und die Aussagen, die davon abh\u00e4ngig sind, ob die Sache als richtig gilt, <strong>body<\/strong>. Der body muss <strong>einger\u00fcckt<\/strong> sein, was hei\u00dft, dass extra Leerzeichen am Anfang der Zeile sind (wie im oberen Beispiel). Der body kann eigentlich mehr als eine Anweisung enthalten: um dies zu tun, schreibe mehrere einger\u00fcckte Zeilen. Hier ist ein Beispiel:<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=birthYear+%3D+1928%0AcurrentYear+%3D+2013%0Aage+%3D+currentYear+-+birthYear%0Aif+age+%3E+65%3A%0A++++print%28%27Enjoy+retirement%21%27%29%0A++++print%28%27You+turn+100+in%27%2C+100%2BbirthYear%29%0Aprint%28%27You+are%27%2C+age%2C+%27years+old%27%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=480&rightStdout=1'><\/iframe><\/p>\n<ul>\n<li>Versuche, diesen Code in der <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=birthYear%20%3D%201928%0AcurrentYear%20%3D%202013%0Aage%20%3D%20currentYear%20-%20birthYear%0Aif%20age%20%3E%2065%3A%0A%20%20%20%20print%28%27Enjoy%20retirement%21%27%29%0A%20%20%20%20print%28%27You%20turn%20100%20in%27%2C%20100%2BbirthYear%29%0Aprint%28%27You%20are%27%2C%20age%2C%20%27years%20old%27%29%0A\" target=\"_blank\">Konsole<\/a> oder im Visualisierer mit Geburtsjahr 1982 laufen zu lassen. Beide der einger\u00fcckten Zeilen werden \u00fcbersprungen.<\/li>\n<\/ul>\n<p>In der n\u00e4chsten \u00dcbung bitten wir dich, ein kurzes Programm mit Boolean-Vergleichen zu schreiben. Im oberen Beispiel sahen wir bereits, dass \"<code>if x &gt; y:<\/code>\" \"falls <code>x<\/code> gr\u00f6\u00dfer als <code>y<\/code>\" bedeutet. Vielleicht findest du auch die folgenden Operationen n\u00fctzlich:<\/p>\n<ul>\n<li><code>if x &lt; y:<\/code> hei\u00dft \"falls x kleiner als y\"<\/li>\n<li><code>if x &gt;= y:<\/code> hei\u00dft \"falls x gr\u00f6\u00dfer oder gleich y\"<\/li>\n<li><code>if x &lt;= y:<\/code> hei\u00dft \"falls x kleiner oder gleich y\"<\/li>\n<\/ul>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox0'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>IHOPython<\/span><\/div>Schreibe ein Programm, welches einen Integer von input durch <code>pancakes = int(input())<\/code> einliest. Falls <code>pancakes<\/code> mehr als 3 ist, gib <code>Yum!<\/code> aus und falls <code>pancakes<\/code> nicht gr\u00f6\u00dfer als 3 ist, gib stattdessen <code>Still hungry!<\/code> aus. <a class=\"hintlink\"  id=\"hintlink1\">Hinweis<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput0\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch0\" value=\"Input Switch\" onclick=\"pbInputSwitch(0,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(0)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(0,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect0' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(0,'6.ihop')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(0);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"636be973a0d9957d41870ef84743b960\"\/>\n<div id='pbresults0' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(0,\"N\");<\/script>\n<\/p>\n<h2>Struktur einer if-Anweisung<\/h2>\n<ul>\n<li>Die erste Zeile,\n<pre>if \u00abcondition\u00bb:<\/pre>hat drei Teile: das Wort <code>if<\/code>, die <code>\u00ab<span style=\"line-height: 22px;\">condition<\/span>\u00bb<\/code> (auf Deutsch: Bedingung), welche ein True\/False-Ausdruck sein muss (mehr dar\u00fcber unten), und einen Doppelpunkt <code>:<\/code><\/li>\n<li>Dann der body bestehend aus einer oder mehreren einger\u00fcckten Zeilen. Die Zahl der Leerzeichen (die Gr\u00f6\u00dfe der Einr\u00fcckung) ist egal. Ist sie aber uneinheitlich, wird <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=birthYear%20%3D%201928%0AcurrentYear%20%3D%202013%0Aage%20%3D%20currentYear%20-%20birthYear%0Aif%20age%20%3E%2065%3A%0A%20%20print%28%27Enjoy%20retirement%21%27%29%0A%20%20%20%20print%28%27You%20turn%20100%20in%27%2C%20100%2BbirthYear%29%0Aprint%28%27You%20are%27%2C%20age%2C%20%27years%20old%27%29%0A\" target=\"_blank\">ein Fehler verursacht<\/a>, da Python die Gr\u00f6\u00dfe der Einr\u00fcckung benutzt, um zu bestimmen, wo der body startet und endet.<\/li>\n<\/ul>\n<p>Der body einer if-Anweisung ist ein Beispiel eines \"Blocks\". Bl\u00f6cke werden sp\u00e4ter an vielen verschiedenen Stellen benutzt, z.B. bei \"for-Schleifen\" und der Definition eigener Funktionen. (Bestimmte andere Programmiersprachen geben Bl\u00f6cke durch geschweifte Klammern <code><code>{}<\/code><\/code> an.)<\/p>\n<p>Die n\u00e4chste \u00dcbung sind eigentlich 4 \u00dcbungen in einer. Du wirst die Zeilen eines Programms arrangieren, um mehrere verschiedene Ergebnisse zu erhalten.<br \/>\n<em>Klicke auf die horizontalen Rahmen, um jeden Teil zu \u00f6ffnen; du kannst das in beliebiger Reihenfolge machen. Wie gew\u00f6hnlich, pack und lass die Zeilen los (drag and drop), um sie neu zu arrangieren.<\/em><\/p>\n<div class=\"accordion\"><br \/>\n<div class='collapseContain hiding'>\n<div class='collapseHead'><span class='icon'><\/span>\nTeil 1: Unerwartete Einr\u00fcckung\n<\/div>\n<div class='collapseBody'><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral scramble' id='pybox2'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Scramble Exercise: <\/span><span class='title'>Unerwartete Einr\u00fcckung<\/span><\/div>Du musst den folgenden Fehler verursachen: <\/p>\n<pre>IndentationError: unexpected indent<\/pre> <ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble2\">\n <li class=\"pyscramble\">if 1000 &lt; 10:<\/li>\n <li class=\"pyscramble\">   print(&quot;message 1&quot;)<\/li>\n <li class=\"pyscramble\">  print(&quot;message 2&quot;)<\/li>\n <li class=\"pyscramble\">if 2 &gt; 1:<\/li>\n <li class=\"pyscramble\">  print(&quot;message 3&quot;)<\/li>\n<\/ul>\n<input type='hidden' id='usercode2' name='usercode2'\/>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"f2465da872946978bca5f25ee05651c0\"\/>\n<div id='pbresults2' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/div><\/div><div class='collapseContain hiding'>\n<div class='collapseHead'><span class='icon'><\/span>\nTeil 2: Unerwartete Ausr\u00fcckung\n<\/div>\n<div class='collapseBody'><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral scramble' id='pybox3'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Scramble Exercise: <\/span><span class='title'>Unerwartete Ausr\u00fcckung<\/span><\/div>Du musst den folgenden Fehler verursachen: <\/p>\n<pre>IndentationError: unindent does not match any outer indentation level<\/pre> <ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble3\">\n <li class=\"pyscramble\">if 1000 &lt; 10:<\/li>\n <li class=\"pyscramble\">  print(&quot;message 2&quot;)<\/li>\n <li class=\"pyscramble\">if 2 &gt; 1:<\/li>\n <li class=\"pyscramble\">  print(&quot;message 3&quot;)<\/li>\n <li class=\"pyscramble\">   print(&quot;message 1&quot;)<\/li>\n<\/ul>\n<input type='hidden' id='usercode3' name='usercode3'\/>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"6f73612cab537a573da1fc4e1d99964d\"\/>\n<div id='pbresults3' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit3\").value = \"Run program\";document.getElementById(\"inputInUse3\").value = \"N\";<\/script>\n<\/div><\/div><div class='collapseContain hiding'>\n<div class='collapseHead'><span class='icon'><\/span>\nTeil 3: Einr\u00fcckung erwartet, aber nicht gefunden\n<\/div>\n<div class='collapseBody'><form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral scramble' id='pybox4'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Scramble Exercise: <\/span><span class='title'>Erwartete Einr\u00fcckung<\/span><\/div>Du musst den folgenden Fehler verursachen: <\/p>\n<pre>IndentationError: expected an indented block<\/pre> <ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble4\">\n <li class=\"pyscramble\">if 1000 &lt; 10:<\/li>\n <li class=\"pyscramble\">   print(&quot;message 1&quot;)<\/li>\n <li class=\"pyscramble\">  print(&quot;message 2&quot;)<\/li>\n <li class=\"pyscramble\">if 2 &gt; 1:<\/li>\n <li class=\"pyscramble\">  print(&quot;message 3&quot;)<\/li>\n<\/ul>\n<input type='hidden' id='usercode4' name='usercode4'\/>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse4\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"4\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"7bc6d70230660daf3c22e7b7186f2ed4\"\/>\n<div id='pbresults4' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit4\").value = \"Run program\";document.getElementById(\"inputInUse4\").value = \"N\";<\/script>\n<\/div><\/div><div class='collapseContain hiding'>\n<div class='collapseHead'><span class='icon'><\/span>\nTeil 4: Ordentliche Einr\u00fcckung\n<\/div>\n<div class='collapseBody'><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral scramble' id='pybox5'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Scramble Exercise: <\/span><span class='title'>Ordentliche Einr\u00fcckung<\/span><\/div>Verursache keine Fehler!<ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble5\">\n <li class=\"pyscramble\">if 1000 &lt; 10:<\/li>\n <li class=\"pyscramble\">   print(&quot;message 1&quot;)<\/li>\n <li class=\"pyscramble\">  print(&quot;message 2&quot;)<\/li>\n <li class=\"pyscramble\">if 2 &gt; 1:<\/li>\n <li class=\"pyscramble\">  print(&quot;message 3&quot;)<\/li>\n<\/ul>\n<input type='hidden' id='usercode5' name='usercode5'\/>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"69217ef5f028657ab522b8b180cf5cd4\"\/>\n<div id='pbresults5' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/div><\/div><\/div>\n<h3>True, False, und bool<\/h3>\n<p>So weit sahen wir im <code>\u00abcondition\u00bb<\/code>-Teil einer <code>if<\/code>-Aussage einen einfachen numerischen Vergleich <code>x &gt; y<\/code>, der wahr ist, wenn <code>x<\/code> gr\u00f6\u00dfer als <code>y<\/code>, und falsch, wenn <code>x<\/code> kleiner oder gleich <code>y<\/code>. Allgemein werden Ausdr\u00fccke, deren Wert entweder wahr oder falsch ist, <strong>Boolean<\/strong> genannt (siehe <a href=\"http:\/\/en.wikipedia.org\/wiki\/George_Boole\">George Boole<\/a>; auf Deutsch sagt man auch \"boolesch\"). In Python wird der <code>bool<\/code>-Typ benutzt, um Werte von Boolean-Ausdr\u00fccken zu repr\u00e4sentieren. F\u00fcr diesen Typ existieren nur zwei Werte: <code>True<\/code> und <code>False<\/code>.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox6'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Boolean expressions<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nprint(2&gt;1)\nb = False\nprint(b)\nprint(type(b), type(2&gt;1), type(True))<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(6)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(6,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"b696c04c9860d8276e0ff89ec3161af2\"\/>\n<div id='pbresults6' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\n<\/p>\n<p>Beachte, dass wenn du in Python die <code>bool<\/code>-Werte <code>True<\/code> und <code>False<\/code> direkt in einem Programm benutzt,<br \/>\nsie mit <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=print%28true%29\" target=\"_blank\">einem Gro\u00dfbuchstaben anfangen m\u00fcssen oder du bekommst eine Fehlermeldung<\/a>.<\/p>\n<h3>Boolean-Vergleiche<\/h3>\n<p>Die Operatoren <code>&gt;<\/code>, <code>&lt;<\/code>, <code>&lt;=<\/code>, und <code>&gt;=<\/code> vergleichen zwei Zahlen und geben einen bool zur\u00fcck. Hier sind zwei andere Wege um Zahlen zu vergleichen:<\/p>\n<ul>\n<li><code>x == y<\/code> ist der <strong>Gleichheitsoperator<\/strong>, er gibt <code>True<\/code> zur\u00fcck, falls <code>x<\/code> und <code>y<\/code> gleich sind<\/li>\n<li><code>x != y<\/code> ist der <strong>Ungleichoperator<\/strong>, er gibt <code>True<\/code> zur\u00fcck, falls <code>x<\/code> and <code>y<\/code> ungleich sind<\/li>\n<li><code>==<\/code> und <code>!=<\/code> funktionieren auch bei strings und anderen Typen von Daten<\/li>\n<\/ul>\n<p>(Hier sind zwei Gleichheitszeichen <code>==<\/code>, denn das (nur) eine hat bei <code>x = \u00abexpression\u00bb<\/code> die Bedeutung \"weise der Variable <code>x<\/code> den Wert von \u00abexpression\u00bb zu\". Die Verwechslung von <code>=<\/code> mit <code>==<\/code> ist eine allgemeine <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=x%20%3D%201%2B1%0Aif%20x%20%3D%202%3A%20%23should%20be%20%3D%3D%0A%20print%28%22Monkey%22%29\" target=\"_blank\">Quelle<\/a> der <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=n%20%3D%2010%0As%20%3D%3D%20n%2An%20%23should%20be%20%3D\" target=\"_blank\">Bugs<\/a>.)<\/p>\n<p data-sider-select-id=\"ba0dff88-9def-47ce-a2e3-15ebcc4613dd\">Hier ist eine \u00dcbung, die eine der neuen Vergleiche beinhalten wird.<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox7'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>What's Your Sign?<\/span><\/div>Schreibe ein Programm, das einen Integer von input einliest, dann eine der W\u00f6rter (mit gro\u00dfem Anfangsbuchstaben) <code>Positive<\/code>, <code>Negative<\/code>, oder <code>Zero<\/code> ausgibt, entsprechend dem, ob die Nummer positiv, negativ oder Null ist.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=20   class='pyboxCode RW'>\nx=int(input())\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput7\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode7' value='x=int(input())\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch7\" value=\"Input Switch\" onclick=\"pbInputSwitch(7,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(7)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(7,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect7' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(7,'6.sign')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(7,descape($('#defaultCode7').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(7);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse7\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"7\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"66131dbd30d3f5d03ae923430cb6b01f\"\/>\n<div id='pbresults7' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(7,\"N\");<\/script>\n<\/p>\n<p>Wir haben gerade das Konzept eines <strong>Blocks<\/strong> eingef\u00fchrt (mehrere zusammengruppierte Zeilen mit dem gleichen Faktor an Einr\u00fcckung).<br \/>\nDu kannst einen Block in einem anderen haben:<\/p>\n<pre>if password=='openSesame':\n  print('User logged on.')\n  if hour&gt;17:\n    print('Good evening!')\n  print('Enter a command:')<\/pre>Hier besteht der \u00e4u\u00dfere Block aus 4 Zeilen und der innere Block aus nur einer Zeile: <pre>\nif password=='openSesame':  <div style=\"margin:0px 20px; margin:0ch 1.9ch; background: #CCF; border: solid blue 1px; border: solid blue 0.1ch;\">print('User logged on.')<br\/>if hour&gt;17:\n<div style=\"margin:0px 20px; margin: 0ch 1.9ch; background: #FCC; border: solid red 1px; border: solid red 0.1ch;\">print('Good evening!')<\/div>print('Enter a command:')<\/div><\/pre><\/p>\n<p><div class='pybox modeNeutral' id='pybox8'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Multiple Choice Exercise: <\/span><span class='title'>Verschachtelte <code>if<\/code>s<\/span><\/div><div>Welche der folgenden Ausgaben ist <strong>nicht<\/strong> m\u00f6glich f\u00fcr dieses Programm?<\/div><label>Your choice: <\/label><select id=\"pyselect8\"><option value=\"d\" selected>Select one<\/option><option value=\"w\">two lines: 'User logged on.' and 'Enter a command:'<\/option><option value=\"w\">three lines: 'User logged on.', 'Good evening!', and 'Enter a command:'<\/option><option value=\"w\">no output<\/option><option value=\"r\">one line: 'User logged on.'<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" name=\"slug\" value=\"6.nested\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(8)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults8\"><\/div><div class=\"epilogue\">Richtig! Falls irgendwelche Nachrichten gezeigt werden, muss es der Fall sein, dass der \u00e4u\u00dfere (blaue) Block ausgef\u00fchrt wurde, oder in anderen Worten das Passwort korrekt war. In diesem Fall, selbst wenn <code>hour&gt;17<\/code> false ist (und der innere Block \u00fcbersprungen wird), wird die Zeile <code>print('Enter a command:')<\/code> des \u00e4u\u00dferen Blocks stets ausgef\u00fchrt.<\/div><\/div>\n<p>Jetzt musst du verschachtelte Bl\u00f6cke verwenden, um einen etwas komplexeren Altersrechner zu programmieren.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform9\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox9'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Alters\u00fcberpr\u00fcfer<\/span><\/div> Schreibe ein Programm, das einen Integer von input einliest, der jemandes Alter repr\u00e4sentiert. Ist das Alter 18 oder gr\u00f6\u00dfer, gib <code>You can vote<\/code> aus. Ist das Alter 0 bis 17 inklusive, gib <code>Too young to vote<\/code> aus. Ist das Alter kleiner als 0, gib <code>You are a time traveller<\/code> aus.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=20   class='pyboxCode RW'>\nage = int(input())\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory9' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput9\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode9' value='age = int(input())\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit9' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch9\" value=\"Input Switch\" onclick=\"pbInputSwitch(9,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(9)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(9,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect9' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(9,'6.agecheck')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(9,descape($('#defaultCode9').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(9);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse9\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"9\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"89cc62e585c9a8b4a300aba352feb9cb\"\/>\n<div id='pbresults9' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(9,\"N\");<\/script>\n<\/p>\n<p>Sp\u00e4ter werden wir in Lektion 9 sehen, dass Situationen mit mehreren Boolean-\u00dcberpr\u00fcfungen vereinfacht werden k\u00f6nnen, und zwar durch den <code>else<\/code> and <code>elif<\/code> (else if)-Befehl, sowie mit den Boolean-Operatoren \"and\", \"or\" und \"not\". Im Moment bist du bereit f\u00fcr die n\u00e4chste Lektion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bis jetzt haben alle unsere Programme ihre Anweisungen der Reihe nach ausgef\u00fchrt, ohne irgendwelche Zeilen zu \u00fcberspringen. Hier ist ein Beispiel; Schritt vor und zur\u00fcck durch den Code mit den Kn\u00f6pfen oder den Pfeiltasten auf der Tastatur. Die neue M\u00f6glichkeit &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/6-de\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2653,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5559","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/5559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/users\/2653"}],"replies":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/comments?post=5559"}],"version-history":[{"count":34,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/5559\/revisions"}],"predecessor-version":[{"id":15918,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/5559\/revisions\/15918"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=5559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}