Westonci.ca is the ultimate Q&A platform, offering detailed and reliable answers from a knowledgeable community. Join our Q&A platform and connect with professionals ready to provide precise answers to your questions in various areas. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.

In "Rabbitville", the rabbit population squares each year. e.g. If you start with 4 rabbits, you'll have 16 in 1 year, and 256 in 2 years. Once the population exceeds 10,000 bunnies, food will be scarce, and population will begin to decrease.


Write a function years_till_no_carrots that includes a WHILE loop. Given a single integer (# of starting rabbits) as an input, your function should output the number of whole years until the number of rabbits becomes larger than 10000. e.g. years_till_no_carrots(4) = 3 (start with 4 rabbits, population greather than 10000 & food scarce in 3 years)



Sagot :

Answer:

In Python:

def years_till_no_carrots(n):

   year = 0

   while(n <= 10000):

       n=n**2

       year = year + 1

   

   return year

Explanation:

First, we define the function

def years_till_no_carrots(n):

Next, the number of years is initialized to 0

   year = 0

The following while loop is iterated until the population (n) exceeds 10000

   while(n <= 10000):

This squares the population

       n=n**2

The number of years is then increased by 1, on each successive iteration

       year = year + 1

   

This returns the number of years

   return year

To call the function from main, use (e.g): print(years_till_no_carrots(4))

Thank you for visiting our platform. We hope you found the answers you were looking for. Come back anytime you need more information. Thanks for using our platform. We aim to provide accurate and up-to-date answers to all your queries. Come back soon. Westonci.ca is your go-to source for reliable answers. Return soon for more expert insights.