{"id":59,"date":"2013-01-14T22:37:59","date_gmt":"2013-01-15T06:37:59","guid":{"rendered":"http:\/\/willclausen.com\/?p=59"},"modified":"2013-01-14T22:37:59","modified_gmt":"2013-01-15T06:37:59","slug":"solution-petals-around-the-rose-dec-18-2012","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=59","title":{"rendered":"Solution: Petals Around The Rose, Dec. 18, 2012"},"content":{"rendered":"<p>Here is my solution to the Programming Praxis problem for Dec. 18, 2012. It is written in Python, which proved to be quite straightforward.<\/p>\n<p>The <a title=\"Petals Around The Rose\" href=\"http:\/\/programmingpraxis.com\/2012\/12\/18\/petals-around-the-rose\/\" target=\"_blank\">task<\/a>:<\/p>\n<p style=\"padding-left: 30px;\">Implement the Petals Around The Rose Game! (see link for more details)<\/p>\n<p>My solution:<\/p>\n<pre class=\"brush: python; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n# Author: Will Clausen\r\n#\r\n# Date: Jan. 14, 2013\r\n#\r\n# This program will implement the Petals Around the Rose game.\r\n# Doing so will solve the Programming Praxis problem from\r\n# Dec. 18, 2012.\r\n# http:\/\/programmingpraxis.com\/2012\/12\/18\/petals-around-the-rose\/\r\n\r\nimport random\r\n\r\ndef PetalsAroundTheRose():\r\n\t# Start by printing the introduction\r\n\tprint &quot;Let's play 'Petals Around The Rose.' &quot;\r\n\tprint &quot;The name of the game is significant. &quot;\r\n\tprint &quot;At each turn I will roll five dice, &quot;\r\n\tprint &quot;then ask you for the score, which &quot;\r\n\tprint &quot;will always be zero or an even number. &quot;\r\n\tprint &quot;After you guess the score, I will tell &quot;\r\n\tprint &quot;you if you are right, or tell you the &quot;\r\n\tprint &quot;correct score if you are wrong. The game &quot;\r\n\tprint &quot;ends when you prove that you know the &quot;\r\n\tprint &quot;secret by guessing the score correctly &quot;\r\n\tprint &quot;six times in a row.&quot;\r\n\r\n\t# Now make a counter to keep track of how many times\r\n\t# the player has submitted a correct answer.\r\n\tcount = 0\r\n\twhile count &lt; 6:\r\n\t\t# Start the game by rolling five dice\r\n\t\trolls = &quot;&quot;\r\n\t\trollsL = &#x5B;]\r\n\t\tfor dice in range(5):\r\n\t\t\tnextRoll = random.randint(1, 6)\r\n\t\t\trolls += &quot; &quot; + str(nextRoll)\r\n\t\t\t# Handy to have these in a list for scoring\r\n\t\t\trollsL += &#x5B;nextRoll]\r\n\r\n\t\tprint\r\n\t\tprint &quot;The five dice are: &quot; + rolls + &quot;.&quot;\r\n\r\n\t\t# Get user input about the score and make sure it's valid\r\n\t\tguess = userInput()\r\n\t\t# Check to make sure the input is valid\r\n\t\tguess = checkInput(guess)\r\n\r\n\t\t# Check the guess against the actual score\r\n\t\tactual = score(rollsL)\r\n\t\tif int(guess) == actual:\r\n\t\t\tcount += 1\r\n\t\t\tprint &quot;Correct&quot;\r\n\t\telse:\r\n\t\t\tcount = 0\r\n\t\t\tprint &quot;The correct score is &quot;  + str(actual) + &quot;.&quot;\r\n\r\n\t# Print out congratulations and closing remarks\r\n\tprint\r\n\tprint &quot;Congratulations! You are now a member &quot;\r\n\tprint &quot;of the Fraternity of the Petals Around &quot;\r\n\tprint &quot;The Rose. You must pledge never to &quot;\r\n\tprint &quot;reveal the secret to anyone.&quot;\r\n\r\n# Helper function for computing the score of the rolls\r\n# in the game. This function reveals the secret!\r\ndef score(rolls):\r\n\tscore = 0\r\n\tfor x in rolls:\r\n\t\tif x%2:\r\n\t\t\tscore += x-1\r\n\r\n\treturn score\r\n\r\n# Helper function that gets and returns valid user input\r\ndef userInput():\r\n\tguess = str(raw_input(&quot;What is the score? &quot;))\r\n\tguess = checkInput(guess)\r\n\treturn guess\r\n\r\n# Helper function that checks the input to ensure it is only\r\n# numerical and repeatedly prompts the user for input that is\r\n# only numerical if the input fails the checks.\r\ndef checkInput(guess):\r\n\tcheck = 0\r\n\twhile check &lt; len(str(guess)):\r\n\t\tif not guess&#x5B;check].isdigit():\r\n\t\t\t# This index will be corrected after this conditional.\r\n\t\t\tcheck = -1\r\n\t\t\tguess = str(raw_input(&quot;Please input only a number: &quot;))\r\n\r\n\t\tcheck += 1\r\n\r\n\tif check == 0:\r\n\t\tprint &quot;Please input SOMETHING! &quot;\r\n\t\tguess = userInput()\r\n\r\n\treturn guess\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is my solution to the Programming Praxis problem for Dec. 18, 2012. It is written in Python, which proved to be quite straightforward. The task: Implement the Petals Around The Rose Game! (see link for more details) My solution: # Author: Will Clausen # # Date: Jan. 14, 2013 # # This program will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[13,9,17,11,12],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-programming-praxis","tag-december-2012","tag-january2013","tag-petals-around-the-rose","tag-programming-praxis-2","tag-solution"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":2,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":61,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/61"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}