Help with assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Help with assignment (/thread-16147.html) |
Help with assignment - dxfrelince - Feb-15-2019 Hi, I want to make a nested indexing on heatingdata = [m,t1,t2,s]. When i Write: heatingdata = [m,t1,t2,s] i=0 For i in range (list(heatingdata)): print(i)it's says name 'm' is not defined. But the teacher has not defined the different letters for us, so how can I make a list on this? Thanks RE: Help with assignment - Larz60+ - Feb-16-2019 if you havent defined m, t1, t2, or s how do you expect python to know what they are? if all you want to do for now is create a list of strings that are equal to 'm', etc. use single or double quotes around each item in the list: heatingdata = ['m', 't1', 't2', 's']But of course now they ahve no relation to variables of same name. RE: Help with assignment - dxfrelince - Feb-16-2019 Thanks Basically,I want to calculate the amount of energy needed to heat a certain amount of water from one temperature to another. I know that Q = m*C*(t2-t1), and also t1<t2 and C=4.19. I want first of all to make a nested indexing: heating_data0 = "m" heating_data1 = "t1" heating_data2 = "t2" heating_data3 = "s" #price heating_data= [heating_data0, heating_data1, heating_data2, heating_data3] print(heating_data) print([heating_data]) But after that, i'm stuck. 1) I want to bring out the values of m, t1, t2 and s 2) Calculate the amount of energy. RE: Help with assignment - Larz60+ - Feb-16-2019 You don't need a list def get_Q(mass, specific_heat, initial_temp, final_temp): return float(mass*specific_heat*(final_temp-initial_temp)) def main(): mass = float(input('Enter mass: ')) specific_heat = float(input('Enter specific heat: ')) initial_temperature = float(input('Enter initial temperature: ')) final_temperature = float(input('Enter final temperature: ')) print('Heat needed: {}'.format(get_Q(mass, specific_heat, initial_temperature, final_temperature))) if __name__ == '__main__': main()output:
RE: Help with assignment - dxfrelince - Feb-17-2019 Thanks :) The problem is that the teacher has not yet gone through that variant and expects us to do it in the basic way I tried to do it in this way, but it's says that "list index out of range". Could you have helped me with that? heating_data = [[50,15,25,10],[100,13,65,15],[25,32,67,80]] for i in range (len(heating_data)): m1 = heating_data[i][0] t1 = heating_data[i][1] t2 = heating_data[i][2] s = heating_data[i][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]]) energi_cost = [[Q,energy,s],[Q,energy,s],[Q,energy,s]] energi_cost = [i][0] energi_cost = [i][1] energi_cost = [i][2] print(energi_cost) round (energy,3) round (price,2) RE: Help with assignment - dxfrelince - Feb-18-2019 heating_data = [[50,15,25,10],[100,13,65,15],[25,32,67,80]] for i in range (len(heating_data)): m1 = heating_data[i][0] t1 = heating_data[i][1] t2 = heating_data[i][2] s = heating_data[i][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]]) energi_cost = [[Q,energy,s],[Q,energy,s],[Q,energy,s]] energi_cost = [i][0] energi_cost = [i][1] energi_cost = [i][2] print(energi_cost) round (energy,3) round (price,2)
RE: Help with assignment - perfringo - Feb-18-2019 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. RE: Help with assignment - dxfrelince - Feb-18-2019 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. Checked out a little youtube. If I write: energy_cost = [i] [0] = Q energy_cost = [i] [1] = energy energy_cost = [i] [2] = priceDoes it look good then? |