Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with assignment
#8
Thanks. I really appreciate the help, since I'm pretty noob on this thing.

But if I now want to create a sublist containing [Q, energy, price], should I re-define this, or can I use what I have done before? :)

(Feb-18-2019, 04:25 PM)perfringo Wrote: You iterate over heating_data elements and overwrite values on every step. Therefore row #13 list elements are equal. Rows #14-16 - you must have name which indexes you want to assign (something = energi_cost[0][1]). You should rethink your logic.

For the first loop some useful techniques (I don't know are you able to use them). You don't need range and len. You iterate over all elements just like this:

heating_data = [[50,15,25,10],[100,13,65,15],[25,32,67,80]]
for row in heating_data:
    m1 = row[0]
    t1 = row[1]
    t2 = row[2]
    s = row[3] 
    C = 4.19
    Q = m1*C*(t2-t1)
    energy = Q*(2.7778*10**-7)
    price = energy * s
    print ([[m1,t1,t2,s,Q,energy,price]])
Of course, using unpacking makes it even more concise:

heating_data = [[50,15,25,10],[100,13,65,15],[25,32,67,80]]
for row in heating_data:
    m1, t1, t2, s = row 
    C = 4.19
    Q = m1*C*(t2-t1)
    energy = Q*(2.7778*10**-7)
    price = energy * s
    print ([[m1,t1,t2,s,Q,energy,price]])
If you want to save values for future references you should create separate list and append values on every iteration. Otherwise (as mentioned earlier) it will be overwritten and only last iteration values will remain.


Checked out a little youtube. If I write:

energy_cost = [i] [0] = Q
energy_cost = [i] [1] = energy
energy_cost = [i] [2] = price
Does it look good then?
Reply


Messages In This Thread
Help with assignment - by dxfrelince - Feb-15-2019, 10:46 PM
RE: Help with assignment - by Larz60+ - Feb-16-2019, 12:35 AM
RE: Help with assignment - by dxfrelince - Feb-16-2019, 11:53 AM
RE: Help with assignment - by Larz60+ - Feb-16-2019, 12:38 PM
RE: Help with assignment - by dxfrelince - Feb-17-2019, 07:24 PM
RE: Help with assignment - by dxfrelince - Feb-18-2019, 11:56 AM
RE: Help with assignment - by perfringo - Feb-18-2019, 04:25 PM
RE: Help with assignment - by dxfrelince - Feb-18-2019, 09:16 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020