Will Clausen's Website

Categories
WCW

Solution: Petals Around The Rose, Dec. 18, 2012

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 implement the Petals Around the Rose game.
# Doing so will solve the Programming Praxis problem from
# Dec. 18, 2012.
# http://programmingpraxis.com/2012/12/18/petals-around-the-rose/

import random

def PetalsAroundTheRose():
	# Start by printing the introduction
	print "Let's play 'Petals Around The Rose.' "
	print "The name of the game is significant. "
	print "At each turn I will roll five dice, "
	print "then ask you for the score, which "
	print "will always be zero or an even number. "
	print "After you guess the score, I will tell "
	print "you if you are right, or tell you the "
	print "correct score if you are wrong. The game "
	print "ends when you prove that you know the "
	print "secret by guessing the score correctly "
	print "six times in a row."

	# Now make a counter to keep track of how many times
	# the player has submitted a correct answer.
	count = 0
	while count < 6:
		# Start the game by rolling five dice
		rolls = ""
		rollsL = []
		for dice in range(5):
			nextRoll = random.randint(1, 6)
			rolls += " " + str(nextRoll)
			# Handy to have these in a list for scoring
			rollsL += [nextRoll]

		print
		print "The five dice are: " + rolls + "."

		# Get user input about the score and make sure it's valid
		guess = userInput()
		# Check to make sure the input is valid
		guess = checkInput(guess)

		# Check the guess against the actual score
		actual = score(rollsL)
		if int(guess) == actual:
			count += 1
			print "Correct"
		else:
			count = 0
			print "The correct score is "  + str(actual) + "."

	# Print out congratulations and closing remarks
	print
	print "Congratulations! You are now a member "
	print "of the Fraternity of the Petals Around "
	print "The Rose. You must pledge never to "
	print "reveal the secret to anyone."

# Helper function for computing the score of the rolls
# in the game. This function reveals the secret!
def score(rolls):
	score = 0
	for x in rolls:
		if x%2:
			score += x-1

	return score

# Helper function that gets and returns valid user input
def userInput():
	guess = str(raw_input("What is the score? "))
	guess = checkInput(guess)
	return guess

# Helper function that checks the input to ensure it is only
# numerical and repeatedly prompts the user for input that is
# only numerical if the input fails the checks.
def checkInput(guess):
	check = 0
	while check < len(str(guess)):
		if not guess[check].isdigit():
			# This index will be corrected after this conditional.
			check = -1
			guess = str(raw_input("Please input only a number: "))

		check += 1

	if check == 0:
		print "Please input SOMETHING! "
		guess = userInput()

	return guess

Leave a Reply

Your email address will not be published. Required fields are marked *