In the English fairy tale, Jack and the beanstalk, Jack is a young, poor boy living with his mother and a dairy cow on a farm cottage. The cow’s milk is their only source of income. When the cow stops giving milk, Jack’s mother tells him to take her to the market to be sold. On the way, Jack meets a curious man who offers magic beans in exchange for the cow, and surprisingly Jack makes the trade. When he arrives home without any money, his mother becomes angry, throws the beans out of the window, and sends Jack to bed without dinner.

During the night, the magic beans cause a gigantic beanstalk to grow outside Jack’s window. Let’s stop the fairy tale story here and focus on the magic beanstalk.

By studying its growth we found out the beanstalk was 100cm high just one hour after Jack’s mother sent the bean out of the window. After that the beanstalk grew at the following rate:

**Every hour the beanstalk grew by 150% and gained an extra 30cm.**

So we can plot the following growth table and chart:

Hours | Height Calculation | Beanstalk Height (in cm) |

1 | 100 | 100 |

2 | 100 x 1.5 + 30 |
180 |

3 | 180 x 1.5 + 30 |
300 |

4 | 300 x 1.5 + 30 |
480 |

5 | 480 x 1.5 + 30 |
750 |

6 | 750 x 1.5 + 30 |
1155 |

… | … |
… |

Using this information, can you work out the height of the magic beanstalk after 7 hours?

The next morning, the beanstalk was so high it could reach the sky! Can you work out the height of the beanstalk after 12 hours?

#### Using an algorithm…

To calculate the height of the beanstalk at any given time/hour we have decided to use an algorithm as follows:

#### Python Code

Your task is to implement this algorithm using Python and calculate the height of the beanstalk after 12 hours or after a full day (24 hours).

[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!Find out how to become a member:

➤ Members' Area

#### Solution...

You are viewing this solution as part of your full membership subscription!#### Python Code

#Jack and the beanstalk - www.101computing.net/jack-and-the-beanstalk-python-challenge/ print("The Magic Beanstalk") height = 100 hours = 0 hours = int(input("Enter a number of hours:")) print("After 1 hour,the beanstalk was 100cm tall.") for i in range(2,hours + 1): height = height * 1.5 + 30 print("After " + str(i) + " hours, the beanstalk was " + str(height) + "cm tall.") print("and it's still growing!")