Python - Control Flow Projects
Noted from Learn Python 3 | Codecademy
Project: Magic 8-Ball
The Magic 8-Ball is a popular toy developed in the 1950s for fortune-telling or advice seeking.
Write a magic8.py Python program that can answer any “Yes” or “No” question with a different fortune each time it executes.
We’ll be using the following 9 possible answers for our Magic 8-Ball:
Yes - definitely.
It is decidedly so.
Without a doubt.
Reply hazy, try again.
Ask again later.
Better not tell you now.
My sources say no.
Outlook not so good.
Very doubtful.
The output of the program will have the following format:
[Name] asks: [Question]
Magic 8-Ball’s answer: [Answer]
For example:
Joe asks: Is this real life?
Magic 8-Ball's answer: Better not tell you now
import random
name = "Tim"
question = "Are you working hard enough?"
answer = ""
random_number = random.randint(1,9)
print(random_number, "-")
if random_number == 1:
answer = "Yes - definitely"
elif random_number == 2:
answer = "It is decidedly so."
elif random_number == 3:
answer = "Without a doubt."
elif random_number == 4:
answer = "Reply hazy, try again."
elif random_number == 5:
answer = "Ask again later."
elif random_number == 6:
answer = "Better not tell you now."
elif random_number == 7:
answer = "My sources say no."
elif random_number == 8:
answer = "Outlook not so good."
elif random_number == 9:
answer = "Very doubtful."
else:
print("ERROR")
print(f"{name} asks: {question}")
print(f"Magic 8-Ball's answer: {answer}")
Project: Sal's Shipping
Sal runs the biggest shipping company in the tri-county area, Sal’s Shippers. Sal wants to make sure that every single one of his customers has the best, and most affordable experience shipping their packages.
In this project, you’ll build a program that will take the weight of a package and determine the cheapest way to ship that package using Sal’s Shippers.
Sal’s Shippers has several different options for a customer to ship their package:
- Ground Shipping, which is a small flat charge plus a rate based on the weight of your package.
- Ground Shipping Premium, which is a much higher flat charge, but you aren’t charged for weight.
- Drone Shipping (new), which has no flat charge, but the rate based on weight is triple the rate of ground shipping.
Here are the prices:
Ground Shipping
Weight of Package | Price per Pound | Flat Charge |
---|---|---|
2 lb or less | $1.50 | $20.00 |
Over 2 lb but less than or equal to 6 lb | $3.00 | $20.00 |
Over 6 lb but less than or equal to 10 lb | $4.00 | $20.00 |
Over 10 lb | $4.75 | $20.00 |
Drone Shipping
Weight of Package | Price per Pound | Flat Charge |
---|---|---|
2 lb or less | $4.50 | $0.00 |
Over 2 lb but less than or equal to 6 lb | $9.00 | $0.00 |
Over 6 lb but less than or equal to 10 lb | $12.00 | $0.00 |
Over 10 lb | $14.25 | $0.00 |
Ground Shipping Premium
Flat charge: $125.00
Write a shipping.py Python program that asks the user for the weight of their package and then tells them which method of shipping is cheapest and how much it will cost to ship their package using Sal’s Shippers.
# Write a function for the cost of ground shipping.
# This function should take one parameter, weight, and return the cost of shipping a package of that weight.
def ground_cost(weight):
if weight <= 2:
cost = weight * 1.50 + 20
elif 2 < weight <= 6:
cost = weight * 3.00 + 20
elif 6 < weight <= 10:
cost = weight * 4.00 + 20
else:
cost = weight * 4.75 + 20
return cost
#A package that weighs 8.4 pounds should cost $53.60 to ship with normal ground shipping:
print("Ground Shipping cost for 8.4 lbs is $" + str(ground_cost(8.4)))
# Premium ground shipping cost
premium_cost = 125.00
#Write a function for the cost of drone shipping
def drone_cost(weight):
if weight <= 2:
cost = weight * 4.50
elif 2 < weight <= 6:
cost = weight * 9.00
elif 6 < weight <= 10:
cost = weight * 12.00
else:
cost = weight * 14.25
return cost
#A package that weighs 1.5 pounds should cost $6.75 to ship by drone
print("Drone Shipping cost for 1.5 lbs is $" + str(drone_cost(1.5)))
# Using those two functions for ground shipping cost and drone shipping cost,
# as well as the cost of premium ground shipping, write a function that takes one parameter, weight
# and prints a statement that tells the user
# The shipping method that is cheapest.
# How much it would cost to ship a package of said weight using this method.
def cheapest_method(weight):
if premium_cost < ground_cost(weight) and premium_cost < drone_cost(weight):
return f"With {weight} lbs, you should use Premium Shipping with the cost of ${premium_cost}"
elif ground_cost(weight) < premium_cost and ground_cost(weight) < drone_cost(weight):
return f"With {weight} lbs, you should use Ground Shipping with the cost of ${ground_cost(weight)}"
else:
return f"With {weight} lbs, you should use Drone Shipping with the cost of ${drone_cost(weight)}"
# Test
print(cheapest_method(17))
print(cheapest_method(3.2))
print(cheapest_method(4.8))
print(cheapest_method(41.5))