# Have you wondered what would happen if you had an infinite loop? 
# It is important to break away from an infinite loop else, the computer keep processing without any end
# Let us take a look at some programs that help break from a loop

'''Task 1: Whats the price '''
print("**** Task 1: ****")
print()
# Write a program that takes the price of goods sold as inputs from the user.
# To stop entering or when the user is done, he or she news to press 0.
# Once the user is done,the program must print
# - Total cost  of the goods 
# - Total number of goods.
x=1
items = 0
cost = 0
while x != 0:
    x=int(input("Tell me the price of one of your goods, this will stop coming when you enter 0\n"))
    if x != 0:
        items+=1
    cost +=x
print(f"You have bought {items} items at a price of ${cost} ")


'''Task 2: Lemonade and Glasses '''
print("**** Task 2: ****")
print()
# Your friend Sam has a jar with 5 cups of fresh lemonade.  
# Jack has some glasses which hold 1.5 cups each of liquid.  
# How many glasses of lemonade can Jack serve?
from decimal import Decimal
g = Decimal(5)/Decimal(1.5)
print(g)
x=0 
counter = 0
while x <= 5:
    x += 1.5
    counter += 1
print(counter)



'''Task 3: Population Calculator '''
print("**** Task 3: ****")
print()
# Sam is thrilled how he is able to solve problems
# He now wants to solve a population projection problem
# Can you solve it for him?
# He wants to know how long will it take to reach a certain target population (in years), given a 
# - starting population =10000000
# - birthrate=0.015
# - deathrate=0.023
# - Reduction can be taken as 0.1.
# Hint1: Target population can be calculated as population * reduction
# Hint2: Change in population can be calculated using the formula (current_pop * birthrate) - (current_pop * deathrate)
starting_population = 10000000 
birthrate = 0.015
deathrate = 0.023
current_pop = starting_population
years = 0
print(f"The starting population is {starting_population:,}.")
target_input = input("What is your target population? ")
target_population = int(target_input)
if target_population >= starting_population:
    print("The population is already at or below that target!")
else:
    while current_pop > target_population:
        change = (current_pop * birthrate) - (current_pop * deathrate)
        current_pop += change
        years += 1

    print(f"\nResults:")
    print(f"It will take {years} years to reach a population of {int(current_pop):,}.")
'''Great! You are exceptionally good at coming out with programming solutions. Way to go!!'''

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: