{"id":417,"date":"2017-08-07T12:57:51","date_gmt":"2017-08-07T12:57:51","guid":{"rendered":"https:\/\/www.triptera.com.au\/wordpress\/?p=417"},"modified":"2017-08-07T12:57:51","modified_gmt":"2017-08-07T12:57:51","slug":"guessing-number-game-in-python","status":"publish","type":"post","link":"https:\/\/www.triptera.com.au\/wordpress\/2017\/08\/07\/guessing-number-game-in-python\/","title":{"rendered":"Guessing number game in Python"},"content":{"rendered":"<p>This exercise is to write a program which asks the user to guess a number and the program will give hints to help the user guess the correct answer.<\/p>\n<p>To display a prompt waiting for a user to enter some data, we use the <code>input()<\/code> function. The <code>input()<\/code> function provides the data as a string, which is a sequence of letters. To convert to an integer number we use the <code>int()<\/code> function. <\/p>\n<p>To control the flow of the program based on the user&#8217;s answers, we use <code>if<\/code> and <code>elif<\/code> statements (<code>elif<\/code> is short for &#8220;else if&#8221;). <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nanswer=15\r\nguess=None\r\nwhile guess != answer:\r\n    guess=int(input(&quot;Guess a number between 0 and 50 ? &quot;))\r\n    if guess &gt; answer:\r\n        print(&quot;too high&quot;)\r\n    elif guess &lt; answer:\r\n        print(&quot;too low&quot;)\r\nprint(&quot;Yes. Answer is &quot;, answer)<\/pre>\n<h3>Importing more functionality<\/h3>\n<p>Sometimes we want more functionality than is provided in the base python system. In our example we want a random number generator which is available in the <code>random<\/code> module (library). The program becomes a lot more interesting now, because we can&#8217;t see the answer by reading the source code.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport random\r\nanswer=random.randint(0,50)\r\nguess=None\r\nwhile guess != answer:\r\n    guess=int(input(&quot;Guess a number between 0 and 50 ? &quot;))\r\n    if guess &gt; answer:\r\n        print(&quot;too high&quot;)\r\n    elif guess &lt; answer:\r\n        print(&quot;too low&quot;)\r\nprint(&quot;Yes. Answer is &quot;, answer)<\/pre>\n<p>In the previous countdown examples, we can put a timer to ensure the countdown happens one second at a time.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport time\r\nx=10\r\nwhile x&gt;0:\r\n    print(x)\r\n    x-=1\r\n    time.sleep(1)\r\nprint(&quot;blast off&quot;)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This exercise is to write a program which asks the user to guess a number and the program will give hints to help the user guess the correct answer. To display a prompt waiting for a user to enter some data, we use the input() function. The input() function provides the data as a string, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-coderdojo"],"_links":{"self":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":1,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/417\/revisions\/418"}],"wp:attachment":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}