Using Computational Thinking in Simple Challenges
I. Defining the problem
We are going to dive into our first scenario. You are launching a campaign for a product you’ve designed. You’re looking for investments that add up to $100,000 in pledges. You want to create an algorithm that saves information on the pledges made for you, while also providing information about the products each pledge provides. There are some things you need to identify before you write an algorithm:
- How many types of pledges will you run in your campaign?
- Will there be a donation-only pledge? Do donations get anything at all, such as a sticker, electronic certificate, or other product swag?
- What will each of the pledges give the person pledging?
All of these questions will help us plan. But we also have to take into account other things, such as what we need from the algorithm. Is it simply to list the number of pledges or will it also be used to check against an inventory or to create that inventory? Will it count down from a set number of pledges available for each level? Will it automatically update every time someone makes a pledge?
As you know, when we are presented with a problem, it is critical that we identify what the problem really is. We need to set our parameters for the problem in order to provide a solution. In this section, we’re going to want to answer a lot of these questions first. In order to do that, we need to decompose the problem and identify which functionalities in Python would help us solve this problem.
Decomposing the problem and using Python functionalities
We can create an algorithm that is used to design those pledges. We’d be designing an algorithm that tells us how many items we need of each type based on a set number of pledges.
For example, say we have three tiers of pledges and a donation-only pledge. For the highest tier, let’s call that Tier 1 , you get the following:
- Double the item you are selling at a 20% discount
- An accessories bundle for the item, which includes four items
- A carrying case for the item and accessories
- Access to a 1-year membership for the site
The middle tier, or
- The item you are selling at a 20% discount
- A carrying case for the item
- Access to a 6-month membership for the site
The lowest tier, or Tier 3 , gets you the following:
- The item you are selling at a 20% discount
- A carrying case for the item
Let’s say we want an algorithm that will give us the number of items we’d need depending on how many pledges we allow for each tier. The fourth tier , which is donation only, would not affect our algorithm, since this algorithm would be used simply to determine how many of each item we’d need depending on the number of pledges.
But things aren’t as simple as just the numbers. Let’s say that you need $100,000 in pledges, as mentioned earlier in this chapter. Then you’d need to figure out how much you’d charge for the tiers and put a price point on them. You’d need to make enough pledges available to at least reach that goal of $100,000.
You have identified the retail price of your item as $800. Here are the costs for each of the items:
- $640 for the item itself (given a 20% discount, and only applicable to Tiers 1, 2, and 3)
- $100 in accessories (Tier 1 only)
- $40 carrying case (applicable to Tiers 1, 2, and 3)
- Membership costs will be passed at $10 a month (let’s say the original cost is $35 a month)
For Tier 1, the pledge would need to be a minimum of $1,540:
- 640 × 2 = 1280
- 12 × 10 = 120
- 1280 + 120 + 100 + 40 = 1540
For Tier 2, the pledge would need to be a minimum of $740:
- 640 + 40 = 680
- 6 × 10 = 60
- 680 + 60 = 740
For Tier 3, the pledge would need to be a minimum of $680, that is, 640 + 40 = 680.
Now we have to figure out how many of each tier would be available for purchase. But first, let’s round out some of these numbers: Tier 1 will be $1,600, Tier 2 will be $800, and Tier 3 will be $700.
We can write an algorithm to let us know how many pledges we need for each tier. But before we do, let’s talk about Python and the functionalities that we could use. We can create the Tier-3 pledge first, making it a parent group. Then we can create two child classes, Tiers 1 and 2, that have slightly different characteristics and costs. The question we ask ourselves is whether we need to be that detailed on the algorithm or can we just add each tier’s cost/value.
That’s a good question. And the answer is that it depends on what you need this algorithm for. If you’re looking to do the early yet simple calculations and need an algorithm you can run every so often based on current sales/pledges, then you can create something simple with a dictionary or a function. But if you want to use the algorithm for multiple purposes or need to be able to adjust some costs later on, then you may want to code that into the algorithm with all the details.
Why would you need that? Take some of the successful campaigns available. Oftentimes, the original pledges sell out for those campaigns. New pledge tiers then become available and you may want to adjust the price point of those tiers. For example, rather than a 20% discount on the item, the pledge may be for a 15% discount. Other changes may happen, such as selling swag instead of the accessories due to inventory limitations and so on.
The more detailed your algorithm is, the more you can do with it in the long run. But if you do not need all that information, then you may not want to spend as much time creating the algorithms.
Let’s get to work on this algorithm. Take a look at the snippet shown as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
import math
tier1 = 1600
tier2 = 800
tier3 = 700
perc = int(input("What percentage of the 100,000 do you expect will be Tier 1? Type a number between 1 and 100. "))
percentage = perc/100
Tier1 = (100000*percentage)/1600
totalTier1 = math.ceil(Tier1)
print("You will need to sell %s Tier 1 pledges if you want %s percent of your sales to be in Tier 1." % (totalTier1,perc))
Let’s take a look at some of the things we used in the preceding snippet. We had to import the math module so we could use the math.ceil() math function. We used this function in order to round the number of Tier-1 pledges needed up. That’s because if we rounded down, we would not be able to cover the desired percentage. By doing this, we are finding the smallest integer we’ll need to cover the percentage.
In addition, we used the %s placeholders in our print statement to call our strings, then stated the strings to be used.
When we run that program, this is what the output looks like:
1
2
3
4
What percentage of the 100,000 do you expect will be Tier 1?
Type a number between 1 and 100. 45
You will need to sell 29 Tier 1 pledges if you want 45 percent
of your sales to be in Tier 1.
Notice that our print statement uses the percentage entered by the user, in part to ensure that the information matches what was expected. For 45% of the funding to come from Tier-1 pledges, we’d need to sell at least 29 Tier-1 pledges. If we run the math just to verify that this is correct, we see that this information is true:
\(29 × 1600 = 46,400\) \(100,000 × 0.45 = 45,000\)
As you can see, if we sell 29 Tier-1 pledges, we’ll make slightly more than 45% of 100,000.
But let’s say that you also want the algorithm to tell you how many items you need based on the number of Tier-1 pledges. Let’s take a look at the adjusted algorithm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import math
tier1 = 1600
tier2 = 800
tier3 = 700
perc = int(input("What percentage of the 100,000 do you expect will be Tier 1? Type a number between 1 and 100. "))
percentage = perc/100
Tier1 = (100000*percentage)/1600
totalTier1 = math.ceil(Tier1)
print("You will need to sell %s Tier 1 pledges if you want %s percent of your sales to be in Tier 1." % (totalTier1,perc))
print("You will need %s items, %s accessories kits, %s carrying cases, and %s year-long memberships." %(totalTier1*2, totalTier1, totalTier1, totalTier1))
Notice that I only added one more print statement. There are pros and cons to using this algorithm in this way. In this case, I’m only outputting the numbers for each of the tiers. I’m not saving how many items I need anywhere in the algorithm. If we want to do that for future reference, we’ll need to adjust how we get that information and how we save it in the algorithm.
For this algorithm, the output is as follows:
1
2
3
What percentage of the 100,000 do you expect will be Tier 1? Type a number between 1 and 100. 45
You will need to sell 29 Tier 1 pledges if you want 45 percent of your sales to be in Tier 1.
You will need 58 items, 29 accessories kits, 29 carrying cases, and 29 year-long memberships.
Notice that we got the information we wanted. We’ll need 58 items, 29 accessories kits, 29 carrying cases, and 29 year-long memberships. Again, this would be helpful if we were doing a one-off thing or if we didn’t expect any changes. But let’s be clear, that’s almost never the case. We’ll want to make changes. We’ll also need to know information based on Tier-2 and Tier-3 selections. So what can we do?
First, we’ll want to save our numbers. So, we’ll need to add some variables for the items, the accessories kits, the carrying cases, and two variables for the memberships, one for the year-long and one for the 6-month-long memberships. We’ll also need to make a decision about how we want the rest of the pledges broken up. Do we want the other percentage equally split between Tiers 2 and 3? Do we want one-third of what’s left to be Tier 2 and two-thirds to be Tier 3? Let’s go with those numbers. Here’s where we stand now:
- The Tier-1 percentage is chosen by the user as the program is run.
- The Tier-2 percentage will be one-third of the remaining percentage.
- Tier 3 will be two-thirds of the remaining percentage.
Let’s teach this to the algorithm. The following file contains the full, uninterrupted code. We added some text to explain certain sections, as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import math
tier1 = 1600
tier2 = 800
tier3 = 700
perc = int(input("What percentage of the 100,000 do you expect will be Tier 1? Type a number between 1 and 100. "))
percTier1 = perc/100
percTier2 = (100-perc)/3/100
percTier3 = (100-perc-percTier2)/100
Tier1 = (100000*percTier1)/1600
totalTier1 = math.ceil(Tier1)
itemsTier1 = totalTier1*2
accTier1 = totalTier1
cases1 = totalTier1
yearMemb = totalTier1
Tier2 = (100000*percTier2)/800
totalTier2 = math.ceil(Tier2)
itemsTier2 = totalTier2
cases2 = totalTier2
sixMemb = totalTier2
Tier3 = (100000*percTier3)/700
totalTier3 = math.ceil(Tier3)
itemsTier3 = totalTier3
cases3 = totalTier3
totalItems = itemsTier1 + itemsTier2 + itemsTier3
totalAccessories = accTier1
totalCases = cases1 + cases2 + cases3
print("You will need to sell %s Tier 1 pledges if you want %s percent of your sales to be in Tier 1." %(totalTier1, perc))
print("You will need %s Tier 2 pledges and %s Tier 3 pledges to meet or exceed your $100,000 funding goal." % (totalTier2, totalTier3))
print("These percentages are equivalent to %s total items, %s total cases, %s accessories kits, %s year-long memberships, and %s six-month memberships." \
% (totalItems, totalCases, totalAccessories, yearMemb, sixMemb))
While we haven’t printed the details yet for the number of total items or the total cases, we now have them saved into variables. This is what our output looks like now:
1
2
3
4
5
6
What percentage of the 100,000 do you expect will be Tier 1?
Type a number between 1 and 100. 50
You will need to sell 32 Tier 1 pledges if you want 50 percent
of your sales to be in Tier 1.
You will need 21 Tier 2 pledges and 72 Tier 3 pledges to meet
or exceed your $100,000 funding goal.
We should note that we exceed our funding goal because we’ve always been rounding up. That is, rather than using $1,540 for Tier 1, we used $1,600. For the percentage, we’ve been rounding up. All of these will add up to give us a total above $100,000.
Let’s extend the algorithm a little more. The following is only the new snippet from the algorithm we’ve already seen, which contains the total for the items we’ll need:
1
print("These percentages are equivalent to %s total items, %s total cases, %s accessories kits, %s year-long memberships, and %s six-month memberships." %(totalItems, totalCases, totalAccessories, yearMemb, sixMemb))
Notice that we can now call those variables we added in our print function to get the counts we need for our inventory. We would not be able to get those details if we had not defined those items in our algorithm.
Also notice that in our previous snippet, some of the items have the exact same value. However, we still defined them with different variables. Take a look, for example, at cases2 = totalTier2 and sixMemb = totalTier2. Although both have the same values, we want to identify them separately. And maybe that’s not important now, but later on, maybe we’ll run out of cases. Then we’d only want to change the value for the cases and not the 6-month memberships.
Since they’re already split, we can change one without affecting the other. Let’s take a look at what the output looks like for the new print statement:
1
2
3
These percentages are equivalent to 157 total items, 125 total
cases, 32 accessories kits, 32 year-long memberships, and 21
six-month memberships.
Looking at this, you may realize that you only get one carrying case but two items in Tier 1, which is why there’s a different count for those. The accessories and year-long memberships only happen in Tier 1, so it makes sense that those two numbers are the same. The 6-month memberships are only for Tier 2, so that number matches the number of Tier-2 pledges.
As we consider this problem further, we may realize that we may want to save information differently. Maybe rather than asking the user for the percentage of Tier-1 pledges they want, we could ask how many total items they have and then break down the tiers based on that. All of that is possible, so how we define the problem is critical. How we save the information or request input from the user is also just as important. Decomposing a problem is just part of the process of creating and designing the algorithms we need.
II. Generalizing the problem and planning Python algorithms
In the previous section, we were working with an algorithm designed for use in a funding campaign. The algorithm we looked at was problem-specific.
Now let’s try to generalize this problem and understand how we could potentially design a different algorithm. Why would we need that? Think of it as a template. If you run multiple funding campaigns for start-ups, you may want to create a general algorithm that you can then adapt based on the needs of each campaign rather than having to start each campaign over.
You would need to set up some clear parameters and make some decisions. In order to keep this scenario manageable for the book’s purposes, we’re going to limit our choices a bit:
- Every campaign will have between 3 and 5 tiers of pledges, not including donation-only.
- Every tier will ask for the items needed for each tier.
- Each tier option will have a set percentage built in.
If there are three tiers, Tier 1 will be 50% of pledges, Tier 2 will be 30%, and Tier 3 will be 20%. If there are four tiers, Tier 1 will be 40% of pledges, Tier 2 will be 30%, Tier 3 will be 20%, and Tier 4 will be 10%. If there are five tiers, Tier 1 will be 40% of pledges, Tier 2 will be 30%, Tier 3 will be 15%, Tier 4 will be 10%, and Tier 5 will be 5%.
Take a look at the following diagram, which shows the breakdown of the tiers:
Because we are using one algorithm for many possible scenarios, we’ll break down that algorithm piece by piece. The full algorithm is shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import math
numberTiers = int(input("How many tiers of pledges will you offer? Enter a number between 3 and 5 inclusive. "))
# Number of tiers percentages
if numberTiers == 3:
tier1 = .50
tier2 = .30
tier3 = .20
elif numberTiers == 4:
tier1 = .40
tier2 = .30
tier3 = .20
tier4 = .10
elif numberTiers == 5:
tier1 = .40
tier2 = .30
tier3 = .15
tier4 = .10
tier5 = .05
else:
print("Please try again and enter the numbers 3, 4, or 5. ")
# Number of items per tier
if numberTiers == 3:
numTier1Items = int(input("How many items will be provided for a Tier 1 pledge? "))
numTier2Items = int(input("How many items will be provided for a Tier 2 pledge? "))
numTier3Items = int(input("How many items will be provided for a Tier 3 pledge? "))
elif numberTiers == 4:
numTier1Items = int(input("How many items will be provided for a Tier 1 pledge? "))
numTier2Items = int(input("How many items will be provided for a Tier 2 pledge? "))
numTier3Items = int(input("How many items will be provided for a Tier 3 pledge? "))
numTier4Items = int(input("How many items will be provided for a Tier 4 pledge? "))
elif numberTiers == 5:
numTier1Items = int(input("How many items will be provided for a Tier 1 pledge? "))
numTier2Items = int(input("How many items will be provided for a Tier 2 pledge? "))
numTier3Items = int(input("How many items will be provided for a Tier 3 pledge? "))
numTier4Items = int(input("How many items will be provided for a Tier 4 pledge? "))
numTier5Items = int(input("How many items will be provided for a Tier 5 pledge? "))
# Price points for each Tier
if numberTiers == 3:
priceTier1 = int(input("What is the price point of Tier 1? Enter dollar amount without dollar sign. "))
priceTier2 = int(input("What is the price point of Tier 2? Enter dollar amount without dollar sign. "))
priceTier3 = int(input("What is the price point of Tier 3? Enter dollar amount without dollar sign. "))
elif numberTiers == 4:
priceTier1 = int(input("What is the price point of Tier 1? Enter dollar amount without dollar sign. "))
priceTier2 = int(input("What is the price point of Tier 2? Enter dollar amount without dollar sign. "))
priceTier3 = int(input("What is the price point of Tier 3? Enter dollar amount without dollar sign. "))
priceTier4 = int(input("What is the price point of Tier 4? Enter dollar amount without dollar sign. "))
elif numberTiers == 5:
priceTier1 = int(input("What is the price point of Tier 1? Enter dollar amount without dollar sign. "))
priceTier2 = int(input("What is the price point of Tier 2? Enter dollar amount without dollar sign. "))
priceTier3 = int(input("What is the price point of Tier 3? Enter dollar amount without dollar sign. "))
priceTier4 = int(input("What is the price point of Tier 4? Enter dollar amount without dollar sign. "))
priceTier5 = int(input("What is the price point of Tier 5? Enter dollar amount without dollar sign. "))
# Breakdown of number of Tiers based on funding goal
fundGoal = int(input("What is the funding goal for this campaign? Enter dollar amount with no symbols. "))
if numberTiers == 3:
Tier1Total = math.ceil(fundGoal*tier1/priceTier1)
Tier2Total = math.ceil(fundGoal*tier2/priceTier2)
Tier3Total = math.ceil(fundGoal*tier3/priceTier3)
print("For a funding goal of %s with %s tiers, you'll need %s Tier 1 pledges, %s Tier 2 pledges, and %s Tier 3 pledges. " % (fundGoal, numberTiers, Tier1Total, Tier2Total, Tier3Total))
Tier1Items = numTier1Items*Tier1Total
Tier2Items = numTier2Items*Tier2Total
Tier3Items = numTier3Items*Tier3Total
print("For %s Tier 1 pledges, you'll need %s items. For %s Tier 2 pledges, you'll need %s items. For %s Tier 3 pledges, you'll need %s items. " %(Tier1Total, Tier1Items, Tier2Total, Tier2Items, Tier3Total, Tier3Items))
elif numberTiers == 4:
Tier1Total = math.ceil(fundGoal*tier1/priceTier1)
Tier2Total = math.ceil(fundGoal*tier2/priceTier2)
Tier3Total = math.ceil(fundGoal*tier3/priceTier3)
Tier4Total = math.ceil(fundGoal*tier4/priceTier4)
print("For a funding goal of %s with %s tiers, you'll need %s Tier 1 pledges, %s Tier 2 pledges, %s Tier 3 pledges, and %s Tier 4 pledges. " % (fundGoal, numberTiers, Tier1Total, Tier2Total, Tier3Total, Tier4Total))
Tier1Items = numTier1Items*Tier1Total
Tier2Items = numTier2Items*Tier2Total
Tier3Items = numTier3Items*Tier3Total
Tier4Items = numTier4Items*Tier4Total
print("For %s Tier 1 pledges, you'll need %s items. For %s Tier 2 pledges, you'll need %s items. For %s Tier 3 pledges, you'll need %s items. For %s Tier 4 pledges, you'll need %s items. " %(Tier1Total, Tier1Items, Tier2Total, Tier2Items, Tier3Total, Tier3Items, Tier4Total, Tier4Items))
elif numberTiers == 5:
Tier1Total = math.ceil(fundGoal*tier1/priceTier1)
Tier2Total = math.ceil(fundGoal*tier2/priceTier2)
Tier3Total = math.ceil(fundGoal*tier3/priceTier3)
Tier4Total = math.ceil(fundGoal*tier4/priceTier4)
Tier5Total = math.ceil(fundGoal*tier5/priceTier5)
print("For a funding goal of %s with %s tiers, you'll need %s Tier 1 pledges, %s Tier 2 pledges, %s Tier 3 pledges, %s Tier 4 pledges, and %s Tier 5 pledges. " % (fundGoal, numberTiers, Tier1Total, Tier2Total, Tier3Total, Tier4Total, Tier5Total))
Tier1Items = numTier1Items*Tier1Total
Tier2Items = numTier2Items*Tier2Total
Tier3Items = numTier3Items*Tier3Total
Tier4Items = numTier4Items*Tier4Total
Tier5Items = numTier5Items*Tier5Total
print("For %s Tier 1 pledges, you'll need %s items. For %s Tier 2 pledges, you'll need %s items. For %s Tier 3 pledges, you'll need %s items. For %s Tier 4 pledges, you'll need %s items. For %s Tier 5 pledges, you'll need %s items. " %(Tier1Total, Tier1Items, Tier2Total, Tier2Items, Tier3Total, Tier3Items, Tier4Total, Tier4Items, Tier5Total, Tier5Items))
As you can see, there are three conditions after we ask for input. Notice that we converted the input to an integer. This is important, otherwise the conditions will run but the else condition will also run.
Once we have the number of tiers, we’ll need to know the number of items in each tier. We’ll need to ask how many items are chosen for each tier. Let’s take a look at the continuation of the preceding code:
1
2
3
4
5
6
7
8
9
10
#Number of items per tier
if numberTiers == 3:
numTier1Items = int(input("How many items will be provided for a Tier 1 pledge? "))
numTier2Items = int(input("How many items will be provided for a Tier 2 pledge? "))
numTier3Items = int(input("How many items will be provided for a Tier 3 pledge? "))
elif numberTiers == 4:
numTier1Items = int(input("How many items will be provided for a Tier 1 pledge? "))
numTier2Items = int(input("How many items will be provided for a Tier 2 pledge? "))
numTier3Items = int(input("How many items will be provided for a Tier 3 pledge? "))
numTier4Items = int(input("How many items will be provided for a Tier 4 pledge? "))
Notice that we’re only showing the conditions for when the tiers are 3 or 4. The code file will also have the information for the 5 tiers, but it follows what is shown in the preceding code. Notice that the algorithm asks for input for each of the tiers. This will be important when we need to work with numbers and percentages.
In addition, we can include an else statement that allows us to ask the questions again if there is an error in the input. You can add these kinds of conditions to the existing template if you wish. For now, we’re going to continue with the next pieces of information we’ll need to get from the user, such as the price point for each tier.
Now let’s think back to what we could need. We’ll need price points for each tier, which will also be the input requests for a template kind of algorithm. Since each of those will be different for each campaign, we’ll need to leave that up to the user to enter. The input lines will look very similar to the previously shown snippets. Here is what it looks like for 3 tiers:
1
2
3
4
5
#Price points for each Tier
if numberTiers == 3:
priceTier1 = int(input("What is the price point of Tier 1? Enter dollar amount without dollar sign. "))
priceTier2 = int(input("What is the price point of Tier 2? Enter dollar amount without dollar sign. "))
priceTier3 = int(input("What is the price point of Tier 3? Enter dollar amount without dollar sign. "))
Again, notice that we’re using comments to separate each section of the code. As you can see, we are adding information about how much we are charging for each pledge level. The code then continues to do this for the number of tiers, 3, 4, or 5.
As was previously discussed, you may also want to test for errors or provide an alternative to continue running the code after an error is added by the user. We are not addressing those errors in this code but they can certainly be added to improve the user experience with this algorithm. As you can see, we’ve started working on how to generalize this algorithm for multiple scenarios.
In this case, we’re generalizing for multiple uses. But we’ve used a lot of algorithms and seen a lot of scenarios in this book where the generalization of the patterns is much simpler. Generalization can be something as simple as writing an equation with one variable. Or it can be creating an algorithm for a number of circumstances and conditions. That’s why it’s important to identify what our problem is and what exactly we want to accomplish.
In this section, we looked at how and when to get input from the user. We also worked through defining variables in order to store the input and use it in our equations for the algorithm to output the necessary information.
To complete the algorithm, we’ll go to the next section, which is focused on the design and testing of the algorithm.
III. Designing and testing the algorithm
In the previous section, we got a lot of input from the user because we were creating an algorithm that was used as a template for many campaigns. Now we need to use that input in a variety of ways. Here’s what we have so far:
- The number of tiers of pledges
- The percentage breakdown of the tiers
- The number of items for each tier
- The cost of each tier
Now we can do something with all that information. First, let’s talk about what we could want. We could want to test how much we’d make selling a specific quantity of the tiers. We could also break down how many of each tier we’d need based on a funding goal, much as we did in the first part of this chapter.
What would be most helpful? Well, that really depends on what you need. I’m going to say that I want the breakdown based on the funding goal. I want to know how many of each pledge type I will need to make available. So now I have to figure out how I’m going to get that information from the variables I’ve already defined.
We’ll need to have three conditions here as well. And because the variable and how many there are of each variable differs per tier type, we’ll need to make sure we’re accounting for that information. Let’s think about three tiers first. Given the funding goal, here are some of the outputs that can be helpful:
- The number of each tier of pledges to be made available
- The number of items per tier that will need to be in inventory
So how do we figure that out?
Let’s say that we had $50,000 as our funding goal and let’s assume that the Tier-1 cost is $500. Then, we can take the following steps to find the number of Tier-1 pledges needed:
- Multiply the funding goal by the percentage, that is, 50,000 × 0.50 = 25,000.
- Divide the resulting number by the cost of the pledge, that is, 25,000 ÷ 500 = 50.
That means that we’ll need to post 50 pledges for Tier 1. Now let’s assume that the user entered that there were 3 items in Tier 1. Then, this means 50 × 3 = 150 items.
1
2
3
4
5
6
7
#Breakdown of number of Tiers based on funding goal
fundGoal = int(input("What is the funding goal for this campaign? Enter dollar amount with no symbols. "))
if numberTiers == 3:
Tier1Total = math.ceil(fundGoal*tier1/priceTier1)
Tier2Total = math.ceil(fundGoal*tier2/priceTier2)
Tier3Total = math.ceil(fundGoal*tier3/priceTier3)
print("For a funding goal of %s with %s tiers, you'll need %s Tier 1 pledges, %s Tier 2 pledges, and %s Tier 3 pledges. " % (fundGoal, numberTiers, Tier1Total, Tier2Total, Tier3Total))
In the preceding snippet, we have a print function with the number of pledges for each tier, but they’re also saved as functions in our conditional statement. Notice that we’ll now have some output here. We’ll get the number of pledges we’ll need from this snippet, but not the number of items per tier. We’ll add that shortly. For now, here’s what that output looks like when we run the program:
1
2
3
4
5
6
7
8
9
How many tiers of pledges will you offer? Enter a number between 3 and 5 inclusive. 3
How many items will be provided for a Tier 1 pledge? Enter a number between 1 and 3 inclusive. 3
How many items will be provided for a Tier 2 pledge? Enter a number between 1 and 3 inclusive. 2
How many items will be provided for a Tier 3 pledge? Enter a number between 1 and 3 inclusive. 1
What is the price point of Tier 1? Enter dollar amount without dollar sign. 500
What is the price point of Tier 2? Enter dollar amount without dollar sign. 400
What is the price point of Tier 3? Enter dollar amount without dollar sign. 350
What is the funding goal for this campaign? Enter dollar amount with no symbols. 50000
For a funding goal of 50000 with 3 tiers, you'll need 50 Tier 1 pledges, 38 Tier 2 pledges, and 29 Tier 3 pledges.
As you can see, we now know that we need to list 50 Tier-1 pledges at $500, 38 Tier-2 pledges at $400, and 29 Tier-3 pledges at $350 to reach our funding goal. Now we have to figure out how many items we need for each tier given the number of items per tier provided. Here’s what that code will look like:
1
2
3
4
5
6
7
8
9
if numberTiers == 3:
Tier1Total = math.ceil(fundGoal*tier1/priceTier1)
Tier2Total = math.ceil(fundGoal*tier2/priceTier2)
Tier3Total = math.ceil(fundGoal*tier3/priceTier3)
print("For a funding goal of %s with %s tiers, you'll need %s Tier 1 pledges, %s Tier 2 pledges, and %s Tier 3 pledges. " % (fundGoal, numberTiers, Tier1Total, Tier2Total, Tier3Total))
Tier1Items = numTier1Items*Tier1Total
Tier2Items = numTier2Items*Tier2Total
Tier3Items = numTier3Items*Tier3Total
print("For %s Tier 1 pledges, you'll need %s items. For %s Tier 2 pledges, you'll need %s items. For %s Tier 3 pledges, you'll need %s items. " %(Tier1Total, Tier1Items, Tier2Total, Tier2Items,Tier3Total, Tier3Items))
As you can see, now we have another three math equations and a print statement that breaks down the information for us. We’ll get the number of pledges for each tier as well as the number of items we’ll need for each of the tiers. If you wanted even more information from this template, you could include pieces from the first example in this chapter, where we broke down the types of items per pledge. We’ll leave that up to you as a challenge.
For now, here’s what our final output would look like for three tiers and a funding goal of $50,000:
1
2
3
4
5
6
7
8
9
10
How many tiers of pledges will you offer? Enter a number between 3 and 5 inclusive. 3
How many items will be provided for a Tier 1 pledge? 3
How many items will be provided for a Tier 2 pledge? 2
How many items will be provided for a Tier 3 pledge? 1
What is the price point of Tier 1? Enter dollar amount without dollar sign. 500
What is the price point of Tier 2? Enter dollar amount without dollar sign. 400
What is the price point of Tier 3? Enter dollar amount without dollar sign. 350
What is the funding goal for this campaign? Enter dollar amount with no symbols. 50000
For a funding goal of 50000 with 3 tiers, you'll need 50 Tier 1 pledges, 38 Tier 2 pledges, and 29 Tier 3 pledges.
For 50 Tier 1 pledges, you'll need 150 items. For 38 Tier 2 pledges, you'll need 76 items. For 29 Tier 3 pledges, you'll need 29 items.
As you can see, we not only have the information we needed, but we also have variables set up to use if we need to adapt this information. Thinking back to previous chapters and notes that we’ve been discussing, let’s try to determine how else we could now save the information.
The first thing that comes to mind is that we could create a dictionary that stores the information for us. If we did that, then we could recall the information we needed from that dictionary, such as the number of items for one tier, for example. We could also adjust the key-value pairs if we needed to without having to enter the entire thing all over again. Say our initial cost for Tier 1 was $500 but we now need it to be $600, yet the other tiers wouldn’t change. Then we could just adjust that one value.
This scenario would allow you to explore many of the functionalities of the Python programming language we’ve discussed. Take some time to study the code, then make some adjustments and try to use some of your knowledge to improve upon it based on different conditions.
Remember, we are always given problem situations that can be interpreted in different ways. It is up to us to write algorithms that meet our needs and the needs of our clients. Sometimes, we’ll get clarification from our stakeholders directly. Other times, we’ll have to ask for that clarification and/or make some of the assumptions ourselves. What is critical is that we design the algorithms and document our progress so that we can adjust, adapt, and change pieces of our work without having to start over if we don’t get what we need.