{"id":1991,"date":"2024-01-29T18:20:40","date_gmt":"2024-01-29T18:20:40","guid":{"rendered":"https:\/\/www.tutavo.com\/Kachina\/?p=1991"},"modified":"2024-03-02T22:15:45","modified_gmt":"2024-03-02T22:15:45","slug":"learn-python-notes-21-30","status":"publish","type":"post","link":"https:\/\/www.tutavo.com\/Kachina\/learn-python-notes-21-30\/","title":{"rendered":"Learn Python &#8211; NOTES 21 &#8211; 30"},"content":{"rendered":"<p><strong>Program 21 &#8211; Evaluate for Leap Year<\/strong><\/p>\n<p># if the year number isn&#8217;t evenly divisible by four, it&#8217;s a common year;<br \/>\n# otherwise, if the year number isn&#8217;t divisible by 100, it&#8217;s a leap year;<br \/>\n# otherwise, if the year number isn&#8217;t divisible by 400, it&#8217;s a common year;<br \/>\n# otherwise, it&#8217;s a leap year.<\/p>\n<p># output one of two possible messages, which are Leap year or Common year,<br \/>\n# depending on the value entered.<\/p>\n<p># verify if the entered year falls into the Gregorian era,<br \/>\n# and output a warning otherwise: Not within the Gregorian<br \/>\n# calendar period. Tip: use the != and % operators.<\/p>\n<p># == Evaluate of two values are equal<br \/>\n# % Modulus &#8211; Returns remainder ONLY &#8211; Discards value to left<br \/>\n# \/= ?<br \/>\n# != Is not equal to<br \/>\n# \/\/ Truncates and disposes of any remainder<br \/>\n# ** Exponent<\/p>\n<p>year = int(input(&#8220;Enter a year: &#8220;))<br \/>\nevaluation = &#8220;Not yet evaluated&#8221;<\/p>\n<p># Write your code here.<br \/>\n# Sample input: 2000 &#8211; &#8211; &#8211; Expected output: Leap year<br \/>\n# Sample input: 2015 &#8211; &#8211; &#8211; Expected output: Common year<br \/>\n# Sample input: 1999 &#8211; &#8211; &#8211; Expected output: Common year<br \/>\n# Sample input: 1996 &#8211; &#8211; &#8211; Expected output: Leap year<br \/>\n# Sample input: 1580 &#8211; &#8211; &#8211; Expected output: Not within the Gregorian calendar<br \/>\n# Robert Test Data &#8211; &#8211; &#8211; &#8211; 1111, 2222, 2224, 15555, 15556<\/p>\n<p>if year &lt;= 1580:<br \/>\nevaluation = &#8220;Not a Gregorian Date&#8221;<br \/>\nelif year%4 &gt; 0:<br \/>\nevaluation = &#8220;This is a common year&#8221;<br \/>\nelif year%4 == 0:<br \/>\nevaluation = &#8220;This is a leap year&#8221;<br \/>\nelif year%2000 == 0:<br \/>\nevaluation = &#8220;This is a leap year&#8221;<br \/>\nelif year%100 == 0:<br \/>\nevaluation = &#8220;This is a leap year&#8221;<br \/>\nelif year%400 == 0:<br \/>\nevaluation = &#8220;This is a common year&#8221;<br \/>\nelse:<br \/>\nevaluation = &#8220;ERROR DETECTED&#8221;<\/p>\n<p>print (evaluation)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Program 22 &#8211; Send multiple values to multiple variables<br \/>\n<\/strong><\/p>\n<p><code class=\"codep \">x, y, z = 5, 10, 8<\/code><\/p>\n<p><code class=\"codep \">print(x &gt; z) # False<br \/>\nprint((y - 5) == x) # True<\/code><\/p>\n<p>You can also do:&nbsp;<br \/>\n<code class=\"codep \">x, y, z = 5, 10, 8<br \/>\nx, y, z = z, y, x<\/code><\/p>\n<p><strong>Program 23 &#8211; Else and Elif<br \/>\n<\/strong><\/p>\n<p>If&nbsp; True<br \/>\n&nbsp;&nbsp; Executes<br \/>\nIf&nbsp; False<br \/>\n&nbsp;&nbsp; Does not execute<br \/>\nIf&nbsp; True<br \/>\n&nbsp;&nbsp; Executes<\/p>\n<p>Elif will exit if True<\/p>\n<p><strong>Program 24 &#8211;&nbsp; Endless While Loops<\/strong><code class=\"codep syntax-color\"><\/code><\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\"><span class=\"ace_keyword\">while<\/span> <span class=\"ace_constant ace_language\">True<\/span><span class=\"ace_punctuation\">:<\/span><\/div>\n<div class=\"ace_line\"><span class=\"ace_keyword\">print<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_string\">&#8220;I&#8217;m stuck inside a loop.&#8221;<\/span><span class=\"ace_paren ace_rparen\">)<\/span><\/div>\n<\/div>\n<\/div>\n<div>&nbsp;<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">Exit with ^C<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<\/div>\n<p><strong>Program 25 &#8211; Equivalent expressions<br \/>\n<\/strong><\/p>\n<p>Try to recall how Python interprets the truth of a condition, and note that these two forms are equivalent:<br \/>\n<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while number != 0:<\/code> and <code>while number:<\/code>.<br \/>\nThe condition that checks if a number is odd can be coded in these equivalent forms, too:<br \/>\n&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;<code>if number % 2 == 1:<\/code> and <code>if number % 2:<\/code>.<\/p>\n<p><strong>Program 26 &#8211; Incrementing a Counter &#8211; Similar to C<\/strong>&nbsp;<\/p>\n<p>&nbsp;<code class=\"codep copy line-numbers syntax-color\"><span class=\"ace_constant ace_numeric\"><strong><span class=\"ace_identifier\">counter<\/span> <span class=\"ace_keyword ace_operator\">-=<\/span> 1 &nbsp; is the same as&nbsp;&nbsp; counter = counter-1<br \/>\n<span class=\"ace_identifier\">&nbsp;counter<\/span> +<span class=\"ace_keyword ace_operator\">=<\/span> 1 &nbsp; is the same as&nbsp;&nbsp; counter = counter+1<\/strong><\/span><\/code><\/p>\n<p><strong>Program 27 &#8211; Hurkle &#8211; A Game by Robert Andrews<br \/>\n<\/strong><\/p>\n<p>NOTE:&nbsp; This program works with valid entries.&nbsp; It will return an error if you enter anything that is not between 1 and 99.<\/p>\n<p><span style=\"font-family: courier new, courier, monospace;\">import random<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">secret_number = random.randint(1, 99)<\/span><\/p>\n<p><span style=\"font-family: courier new, courier, monospace;\">import sys<br \/>\n# Multi Line Print<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: courier new, courier, monospace;\">print(<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">&#8220;&#8221;&#8221;<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">+============================+<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">| Welcome to Robert&#8217;s Hurkle.|<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">| Guess the Secret Number. |<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">+============================+<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">&#8220;&#8221;&#8221;)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;The Secret Number is &#8221; + str(secret_number))<\/span><\/p>\n<p><span style=\"font-family: courier new, courier, monospace;\">MyGuess = 0<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">guesses = 0<\/span><\/p>\n<p><span style=\"font-family: courier new, courier, monospace;\">while MyGuess != secret_number:<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">MyGuess = int(input(&#8220;Enter your secret number guess: &#8220;))<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">if MyGuess &gt; secret_number: hint=&#8221;Lower&#8221;<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">else: hint= &#8220;Higher&#8221;<\/span><\/p>\n<p><span style=\"font-family: courier new, courier, monospace;\">if MyGuess == secret_number:<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;Congratulations. You guessed correctly.&#8221;)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;The number is &#8221; + str(MyGuess))<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;it took you &#8221; + str(guesses+1) + &#8221; guesses to get it right.&#8221;)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print()<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">elif MyGuess == 0:<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;The number was &#8221; + str(secret_number) + &#8220;.&#8221;)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">sys.exit()&nbsp;&nbsp; # You can also use break<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">else:<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;Wrong. &#8221; + str(MyGuess) + &#8221; is not the number.&#8221;)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;Enter ZERO to give up.&#8221;)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">guesses += 1<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;So far, you guessed &#8221; + str(guesses) + &#8221; times.&#8221;)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print (&#8220;The secret number is &#8221; + hint)<\/span><br \/>\n<span style=\"font-family: courier new, courier, monospace;\">print()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Program 28 &#8211; Loops &#8211; If and For<\/strong><code class=\"codep syntax-color\"><\/code><\/p>\n<ol>\n<li class=\"ace_line\"><span class=\"ace_identifier\">i<\/span> <span class=\"ace_keyword ace_operator\">=<\/span> <span class=\"ace_constant ace_numeric\">0<\/span><code class=\"codep syntax-color\"><\/code><\/li>\n<li class=\"ace_line\"><span class=\"ace_keyword\">while<\/span> <span class=\"ace_identifier\">i<\/span> <span class=\"ace_keyword ace_operator\">&lt;<\/span> <span class=\"ace_constant ace_numeric\">100<\/span><span class=\"ace_punctuation\">:<\/span><code class=\"codep syntax-color\"><\/code><\/li>\n<li class=\"ace_line\"><span class=\"ace_comment\">&nbsp;&nbsp;&nbsp;&nbsp; # do_something()<\/span><code class=\"codep syntax-color\"><\/code><\/li>\n<li class=\"ace_line\"><span class=\"ace_identifier\">&nbsp;&nbsp;&nbsp;&nbsp; i<\/span> <span class=\"ace_keyword ace_operator\">+=<\/span> <span class=\"ace_constant ace_numeric\">1<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li>for i in range(100):<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp; # do_something()<\/li>\n<li>pass<\/li>\n<\/ol>\n<ul>\n<li>for i in range(10):<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp; print(&#8220;The value of i is currently&#8221;, i)&nbsp;&nbsp;&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp; # Note, space added automatically with comma<\/li>\n<\/ul>\n<p>Starts with zero and ends with 9<\/p>\n<p>You can also specify a range by two integers<\/p>\n<p><code class=\"codep \">for i in range(<mark>2, 8<\/mark>):<\/code><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; print(&#8220;The value of i is currently&#8221;, i)<\/p>\n<p><strong>Program 29 -Hurkle &#8211; IMPROVED<br \/>\n<\/strong><\/p>\n<p>import random<br \/>\nsecret_number = random.randint(1, 99)<\/p>\n<p>import sys<\/p>\n<p>print(<br \/>\n&#8220;&#8221;&#8221;<br \/>\n+============================+<br \/>\n| Welcome to Robert&#8217;s Hurkle.|<br \/>\n| Guess the Secret Number. |<br \/>\n+============================+<br \/>\n&#8220;&#8221;&#8221;)<br \/>\nprint (&#8220;The Secret Number is &#8221; + str(secret_number))<\/p>\n<p>MyGuess = 0<br \/>\nguesses = 0<\/p>\n<p>while MyGuess != secret_number:<br \/>\nMyGuess = int(input(&#8220;Enter your secret number guess: &#8220;))<br \/>\nif MyGuess &gt; secret_number: hint=&#8221;Lower&#8221;<br \/>\nelse: hint= &#8220;Higher&#8221;<\/p>\n<p>if MyGuess == secret_number:<br \/>\nprint (&#8220;Congratulations. You guessed correctly.&#8221;)<br \/>\nprint (&#8220;The number is &#8221; + str(MyGuess))<br \/>\nprint (&#8220;it took you &#8221; + str(guesses+1) + &#8221; guesses to get it right.&#8221;)<br \/>\nprint()<br \/>\nelif MyGuess == 0:<br \/>\nprint (&#8220;The number was &#8221; + str(secret_number) + &#8220;.&#8221;)<br \/>\nsys.exit()<br \/>\nelse:<br \/>\nprint (&#8220;Wrong. &#8221; + str(MyGuess) + &#8221; is not the number.&#8221;)<br \/>\nprint (&#8220;Enter ZERO to give up.&#8221;)<br \/>\nguesses += 1<br \/>\nprint (&#8220;So far, you guessed &#8221; + str(guesses) + &#8221; times.&#8221;)<br \/>\nprint (&#8220;The secret number is &#8221; + hint)<br \/>\nprint()<\/p>\n<p><strong>Program 30 &#8211; Time Delay and For Loops<br \/>\n<\/strong><\/p>\n<p>import time<br \/>\nFindMe = &#8220;Ready or not. Here I come.&#8221;<\/p>\n<p># Write a for loop that counts to five.<br \/>\ni=1<br \/>\nwhile i&lt;6:<br \/>\nprint (i,&#8221;Mississippi&#8221;)<br \/>\ntime.sleep(1)&nbsp;&nbsp; # DELAYS PROGRAM 1 SECOND<br \/>\n# Body of the loop &#8211; print the loop iteration number and the word &#8220;Mississippi&#8221;.<br \/>\ni += 1<br \/>\n# Write a print function with the final message.<br \/>\nprint (FindMe)<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Program 21 &#8211; Evaluate for Leap Year # if the year number isn&#8217;t evenly divisible by four, it&#8217;s a common year; # otherwise, if the year number isn&#8217;t divisible by 100, it&#8217;s a leap year; # otherwise, if the year number isn&#8217;t divisible by 400, it&#8217;s a common year; # otherwise, it&#8217;s a leap year. &hellip; <a href=\"https:\/\/www.tutavo.com\/Kachina\/learn-python-notes-21-30\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn Python &#8211; NOTES 21 &#8211; 30&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1991","post","type-post","status-publish","format-standard","hentry","category-educational"],"_links":{"self":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/1991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/comments?post=1991"}],"version-history":[{"count":16,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/1991\/revisions"}],"predecessor-version":[{"id":2047,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/1991\/revisions\/2047"}],"wp:attachment":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/media?parent=1991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/categories?post=1991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/tags?post=1991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}