Posts: 8
Threads: 3
Joined: Feb 2019
Feb-15-2019, 10:46 PM
(This post was last modified: Feb-16-2019, 12:32 AM by Larz60+.)
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
Posts: 12,030
Threads: 485
Joined: Sep 2016
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.
Posts: 8
Threads: 3
Joined: Feb 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.
Posts: 12,030
Threads: 485
Joined: Sep 2016
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:
Output: Enter mass: 750
Enter specific heat: 4.186
Enter initial temperature: 85
Enter final temperature: 170
Heat needed: 266857.5
Posts: 8
Threads: 3
Joined: Feb 2019
Feb-17-2019, 07:24 PM
(This post was last modified: Feb-18-2019, 12:24 AM by Larz60+.)
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)
Posts: 8
Threads: 3
Joined: Feb 2019
Feb-18-2019, 11:56 AM
(This post was last modified: Feb-18-2019, 12:03 PM by dxfrelince.)
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) Output: File "<ipython-input-39-696429c3a249>", line 14, in <module>
energi_cost = [i][1]
IndexError: list index out of range
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 8
Threads: 3
Joined: Feb 2019
Feb-18-2019, 09:16 PM
(This post was last modified: Feb-18-2019, 10:11 PM by dxfrelince.)
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?
|