{"id":2018,"date":"2018-06-06T15:11:38","date_gmt":"2018-06-06T14:11:38","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/?p=2018"},"modified":"2018-06-06T15:13:46","modified_gmt":"2018-06-06T14:13:46","slug":"higher-rock-paper-scissors-solution","status":"publish","type":"post","link":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/2018\/06\/06\/higher-rock-paper-scissors-solution\/","title":{"rendered":"Higher &#8211; Rock Paper Scissors &#8211; Solution"},"content":{"rendered":"<p>So when we implemented the code we got something like this<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#Mr Stratton\r\n\r\nimport random\r\n\r\n#set up variables\r\nrandom.seed\r\nobject=&#x5B;&quot;Rock&quot;,&quot;Paper&quot;,&quot;Scissors&quot;]\r\nplayerChoice=&quot;&quot;\r\ncomputerChoice=&quot;&quot;\r\nwinner=&quot;&quot;\r\n\r\n#Get players choice\r\nplayerChoice=object&#x5B;int(input(&quot;Rock - 1\\nPaper - 2\\nScissors - 3\\n&quot;))-1]\r\n\r\n#Get computer choice\r\ncomputerChoice=object&#x5B;random.randint(0,2)]\r\n\r\n#get winner\r\nif playerChoice==&quot;Rock&quot;:\r\n    if computerChoice==&quot;Paper&quot;:\r\n        winner=&quot;Computer&quot;\r\n    if computerChoice==&quot;Scissors&quot;:\r\n        winner=&quot;Player&quot;\r\n    if computerChoice==playerChoice:\r\n        winner=&quot;Draw&quot;\r\n        \r\nif playerChoice==&quot;Paper&quot;:\r\n    if computerChoice==&quot;Scissors&quot;:\r\n        winner=&quot;Computer&quot;\r\n    if computerChoice==&quot;Rock&quot;:\r\n        winner=&quot;Player&quot;\r\n    if computerChoice==playerChoice:\r\n        winner=&quot;Draw&quot;\r\n        \r\nif playerChoice==&quot;Scissors&quot;:\r\n    if computerChoice==&quot;Rock&quot;:\r\n        winner=&quot;Computer&quot;\r\n    if computerChoice==&quot;Paper&quot;:\r\n        winner=&quot;Player&quot;\r\n    if computerChoice==playerChoice:\r\n        winner=&quot;Draw&quot;\r\n        \r\n\r\n    \r\n#display winner\r\nprint(&quot;\\n&quot;*10)\r\nprint(&quot;Player threw&quot;,playerChoice)\r\nprint(&quot;Computer threw&quot;,computerChoice)\r\n\r\nif winner==&quot;Player&quot;:\r\n    print(&quot;Player is the winner&quot;)\r\n    \r\nif winner==&quot;Computer&quot;:\r\n    print(&quot;Computer is the winner&quot;)\r\n    \r\nif winner==&quot;Draw&quot;:\r\n    print(&quot;Its a draw&quot;)\r\n\r\n\r\n<\/pre>\n<p>However, although it does follow the design from yesterday it isn&#8217;t very effcient.<br \/>\nCan you see a way to make it more effient?<\/p>\n<pre class=\"brush: python; collapse: true; light: false; title: Solution is here; toolbar: true; notranslate\" title=\"Solution is here\">\r\n#Mr Stratton\r\n\r\nimport random\r\n\r\n#set up variables\r\nobject=&#x5B;&quot;Rock&quot;,&quot;Paper&quot;,&quot;Scissors&quot;]\r\nplayerChoice=&quot;&quot;\r\ncomputerChoice=&quot;&quot;\r\nwinner=&quot;Draw&quot;\r\n\r\n#Get players choice\r\nplayerChoice=object&#x5B;int(input(&quot;Rock - 1\\nPaper - 2\\nScissors - 3\\n&quot;))-1]\r\n\r\n#Get computer choice\r\ncomputerChoice=object&#x5B;random.randint(0,2)]\r\n\r\n#get winner\r\nif playerChoice==&quot;Rock&quot;:\r\n    if computerChoice==&quot;Paper&quot;:\r\n        winner=&quot;Computer&quot;\r\n    elif computerChoice==&quot;Scissors&quot;:\r\n        winner=&quot;Player&quot;\r\n\r\n        \r\nelif playerChoice==&quot;Paper&quot;:\r\n    if computerChoice==&quot;Scissors&quot;:\r\n        winner=&quot;Computer&quot;\r\n    elif computerChoice==&quot;Rock&quot;:\r\n        winner=&quot;Player&quot;\r\n\r\n        \r\nelif playerChoice==&quot;Scissors&quot;:\r\n    if computerChoice==&quot;Rock&quot;:\r\n        winner=&quot;Computer&quot;\r\n    elif computerChoice==&quot;Paper&quot;:\r\n        winner=&quot;Player&quot;\r\n\r\n        \r\nelse:\r\n    print(&quot;Error in player choice&quot;)\r\n    \r\n#display winner\r\nprint(&quot;\\n&quot;*10)\r\nprint(&quot;Player threw&quot;,playerChoice)\r\nprint(&quot;Computer threw&quot;,computerChoice)\r\nif winner==&quot;Player&quot;:\r\n    print(&quot;Player is the winner&quot;)\r\nelif winner==&quot;Computer&quot;:\r\n    print(&quot;Computer is the winner&quot;)\r\nelse:\r\n    print(&quot;Its a draw&quot;)\r\n\r\n<\/pre>\n<p><!--more--><\/p>\n<p>That&#8217;s right use nested IFs and ELIF to reduce the number of IFs that need to be evaluated.<br \/>\nI have also made the code more effient (reduced the number of lines of code) in another way in the example above. Can you spot where?<\/p>\n<pre class=\"brush: python; collapse: true; light: false; title: Solution is here; toolbar: true; notranslate\" title=\"Solution is here\">\r\n# I changed line 9 to assume it was always a draw which allowed me to remove \r\n# 6 lines of code that delt with this.\r\n# It is more effient but is it good programming practice?\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So when we implemented the code we got something like this #Mr Stratton import random #set up variables random.seed object=&#x5B;&quot;Rock&quot;,&quot;Paper&quot;,&quot;Scissors&quot;] playerChoice=&quot;&quot; computerChoice=&quot;&quot; winner=&quot;&quot; #Get players choice playerChoice=object&#x5B;int(input(&quot;Rock &#8211; 1\\nPaper &#8211; 2\\nScissors &#8211; 3\\n&quot;))-1] #Get computer choice computerChoice=object&#x5B;random.randint(0,2)] #get winner if playerChoice==&quot;Rock&quot;: if computerChoice==&quot;Paper&quot;: winner=&quot;Computer&quot; if computerChoice==&quot;Scissors&quot;: winner=&quot;Player&quot; if computerChoice==playerChoice: winner=&quot;Draw&quot; if playerChoice==&quot;Paper&quot;: if computerChoice==&quot;Scissors&quot;: winner=&quot;Computer&quot; &hellip;<\/p>\n","protected":false},"author":32,"featured_media":1386,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[432098,17932,17009],"tags":[432099,125715,13697,33634],"class_list":["post-2018","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-higher","category-computational-constructs","category-software-design-devlopment","tag-elif","tag-python","tag-random","tag-selection"],"jetpack_featured_media_url":"https:\/\/blogs.glowscotland.org.uk\/nl\/public\/ColtnessHS-ComputingScience\/uploads\/sites\/12638\/2016\/09\/Slide4.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/comments?post=2018"}],"version-history":[{"count":8,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2018\/revisions"}],"predecessor-version":[{"id":2026,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2018\/revisions\/2026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/media\/1386"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/media?parent=2018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/categories?post=2018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/tags?post=2018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}