← Back to all posts
anyone know how to solve this ?

A farrier was contacted one Sunday and asked to shoe a horse. Because Sunday was his day off, the farrier advised the rider to come back tomorrow, and that it would cost \$150. But the rider kept insisting that the horse needed to be shod immediately, until eventually the farrier wore down and made the following offer: he would shoe the horse for free, but the rider needed to buy the nails from him and would be charged \$0.01 for the first nail, and twice the price of the previous nail for each nail after that (e.g. the second nail would cost \$0.02, the third \$0.04, and so on). Given that a horse needs four shoes, and each shoe needs six nails, complete the following program to determine the total cost for the rider. It just goes to show how quickly things can grow with exponential growth!

total_cost = 0
cost_of_nail = 1
for horse_foot in range(4):
for nail in range(6):

``````    # TODO: Add price of current nail to total_cost and determine the cost of the next nail
pass``````

print("The total cost to shoe the horse would be \${0:.2f}".format(total_cost / 100))

WillKoehrsen (1)

In addition to the solution I have below, you could simplify things by recognizing that the nail prices are just increasing powers of 2.

``````n_feet = 4
n_nails = 6
total_cost = sum([2**n for n in range(n_feet * n_nails)])``````
WillKoehrsen (1)

You need to add the current cost of the nail to the total cost and then update the cost of the nail to double the previous amount.

``````total_cost = 0
cost_of_nail = 1
for horse_foot in range(4):
for nail in range(6):
total_cost += cost_of_nail
cost_of_nail *= 2``````

Hope this helps!

IbraheemRodrigues (86)

Not really helping you fix your code, which I think is what you really want to do, but you could use this to dodge the for loop:

`foot = 0.01 * (2**n)-1` Where n is the number of nails for the foot

PYer (3510)

I am to busy coding, sorry.