0

Where Weights are user inputs, how would I best go about creating the following output:

biz = # of businesses

Tier  Weights   Price  Businesses  Revenue
Tier1   WT1     XXX    WT1*biz     XXX(WT1*biz)
Tier2   WT2     YYY    WT2*biz     YYY(WT2*biz)
Tier3   WT3     ZZZ    WT3*biz     ZZZ(WT3*biz)

Would I best be served by following the example in this question: Python List as variable name

Although I'd appreciate the code, I'd rather just have some guidance and some knowledge dropped on me so I can become a better developer.

TIA

What I ended up doing:

Tier0 = ['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'] 
Tier1 = ['Tier 1', 180,]
Tier2 = ['Tier2', 300]
Tier3 = ['Tier3', 450]
Tier4 = ['Tier4', 600]
Tier5 = ['Tier5', 750]

data = []
data.append(Tier0)
data.append(Tier1)
data.append(Tier2)
data.append(Tier3)
data.append(Tier4)
data.append(Tier5)    
data

for Tier1 in data[1:]: #what does data [1:] refer to?
    Tier1.insert(1, float(input('Enter the weighted value of Tiers 1-5 as a decimal: ')))
    Tier1.insert(3, Tier1[1] * NationalBusiness)#calculates the number of businesses
    Tier1.insert(4, Tier1[2] * Tier1[1] * NationalBusiness)#calculates the revenue
Community
  • 1
  • 1
  • 1
    When you mean `Weights` are user inputs, rest of the data already exists? – shaktimaan Jun 11 '14 at 04:35
  • Are the weights all different values? Regardless with the other values depending on weight you may want to think about making an object that calculates and stores all those values on instantiation for given weight/weights – GleasonK Jun 11 '14 at 04:47
  • @GleasonK Yes, there will be 5 different weights totaling 1 (or 100%). So you would suggest making a class to store the calculated values? – user3728061 Jun 11 '14 at 05:35
  • @shaktimaan Yes. The "price" is pre-defined and the columns of "Businesses" and "Revenue" can only be calculated once the "weights" are given. – user3728061 Jun 11 '14 at 05:39
  • @user3728061 Updated my answer correspondingly – shaktimaan Jun 11 '14 at 06:17

2 Answers2

0

Make a loop that receives a user input, stores it in a list and (if not empty) breaks the loop:

weights = []
while True:
    weight = float(raw_input("Insert weight: "))
    if not weight:
        break
    weights.append(weight)
print weights

Then, produce the desired output

Magnun Leno
  • 2,728
  • 20
  • 29
0

Assuming you have a list say, [10, 30, 40] and you want to take an input from the user and insert it at the second index. This is what you do:

a_list = [10, 30, 40]
a_list.insert(1, int(raw_input('Enter the value for weight')))

And whatever the user inputs becomes the second element. If the user inputs 20, a_list now becomes:

[10, 20, 30, 40]

Assuming you have a data as a list of lists as:

data = [
        ['Tier', 'Weights', 'Price', 'Businesses', 'Revenue'],
        ['Tier1', 30],
        ['Tier2', 30],
        ['Tier3', 30]
    ]

then this code, inserts user input as the second element of each of the sub-lists:

for a_list in data[1:]:
    a_list.insert(1, int(raw_input('Enter the value for weight')))
    a_list.append(a_list[1] * biz)
    a_list.append(a_list[2] * a_list[1] * biz)
shaktimaan
  • 11,962
  • 2
  • 29
  • 33
  • Your updated answer is what I ended up doing. But why would you use insert on the last two elements instead of append? Just curious. – user3728061 Jun 11 '14 at 13:03
  • @user3728061 Agreed, I just copy pasted the previous version of my answer. You are right, the last two elements could use the append. – shaktimaan Jun 11 '14 at 19:19