-1

I've just started learning python recently because of work, and I'm struggling with a for loop in my code. I want my program to output values for amount of ammonia produced hourly, which is dependent on the cells in Excel (attached below), and I need the columns for 'H2 consumed by NH3' and 'Actual Stored' as lists in Python in order to move forward. However, when I run this code, the process is constantly running and doesn't stop, which made me realize I've messed up somewhere but I can't tell where.

hourly_H2_prod = [hourly_elec*1000/avg_pwr_use for hourly_elec in elec_list]
H2_sum = sum(hourly_H2_prod)
avg_hourly_H2_consumed = avg_hourly_NH3_prod*3/17.31

init_H2_stored = H2_sum/365*Storage_days        #highlighted cell in Excel screenshot

actual_H2_stored = [init_H2_stored]
hourly_H2_consumed = []
for i in range(1,len(hourly_H2_prod)):
    for j in range(len(actual_H2_stored)):
        hourly_H2_consumed.append(max(min(avg_hourly_H2_consumed,actual_H2_stored[j]),0))
        actual_H2_stored.append(max(min(init_H2_stored,actual_H2_stored[j]+hourly_H2_prod[i]-hourly_H2_consumed[j]),0))

I have already managed to get the list format for the 'hourly H2 production' column

(this is to show how the values are dependent on each other, for actual-H2-stored)

(how values depend for hourly-H2-consumed)

I think I'm messing up when I need to iterate using the previous variable as shown in the excel formula. I also hope I've explained everything fully, I appreciate any help!

3
  • Please use copy&paste instead of images - see How to Ask and meta.stackoverflow.com/questions/285551/… Commented Jul 29, 2021 at 19:55
  • It would not let me do that since I am a new user and need to have 10 reputation points or something Commented Jul 29, 2021 at 19:56
  • rather than for i in range(1,len(X)) do for ii,value in enumerate(X) it's more pythonic. Commented Jul 29, 2021 at 20:06

2 Answers 2

0

I'm trying to see where your error is, but I cannot run your code.
I think that the issue is truly in that nested "j" for loop. As of python 3 a range is a generator, so it's possible that adding to that list in the nested loop is going to create an endless loop because the len function may be called each iteration. Try replacing it with this :for j in list(range(len(actual_H2_stored))):

I added the list to the range so that it'll fully preprocess your list's length before you start iteration.

Sign up to request clarification or add additional context in comments.

3 Comments

Tried replacing that line as you mentioned and the code still doesn't run. I also tried removing that line altogether, replaced the j in front of actual_H2_stored with [i-1] and the j in front of hourly_H2_consumed with [i], and I got an IndexError: list index out of range.
I figured it out, posted it as an answer so that others can see, thank you for the tip!
I’m sorry that I could not provide a concrete answer. Modifying a data structure while it’s being iterated over (a for-loop) is a general “code smell” (thing to be avoided, and a place to look for bugs). I’m glad that you found your answer. Happy hacking.
0

I figured it out, @VonWooDSoN you were right the nested loop did mess everything up. I removed the line altogether and instead just put in this, and it worked perfectly and now I'm getting the values in the list form I need, so thank you for the tip!

for i in range(1,len(hourly_H2_prod)):
    hourly_H2_consumed.append(max(min(avg_hourly_H2_consumed,actual_H2_stored[i-1]),0))
    actual_H2_stored.append(max(min(init_H2_stored,actual_H2_stored[i-1]+hourly_H2_prod[i]-hourly_H2_consumed[i-1]),0))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.