Danny
Danny

Reputation: 157

Storing Output to Variable and Printing Output Later

I am having trouble with a piece of my code. I want to store an output statement to a variable. I want to put it outside the for loop so it outputs the statement at the end of my code rather than inside the loop. Is this possible. This is what I tried but I only get one output where multiple statements should be outputting.

outputs=[]
if Year in mydict and data_location in mydict[Year]:  
    busses_in_year = mydict[Year]
    #print("Here are all the busses at that location for that year and the new LOAD TOTAL: ")
    #print("\n")

    #Busnum, busname,scaled_power read from excel sheet matching year and location

    for busnum,busname,scaled_power in busses_in_year[data_location]:
        scaled_power= float(scaled_power)
        busnum = int(busnum)
        output='Bus #: {}\t Area Station: {}\t New Load Total: {} MW\t'
        formatted = output.format(busnum, busname, scaled_power)
        outputs.append(formatted)

        psspy.bsys(1,0,[0.0,0.0],0,[],1,[busnum],0,[],0,[])
        psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
        psspy.scal_2(0,1,2,[0,1,0,1,0],[scaled_power,0.0,0,-.0,0.0,-.0,0])
        psspy.fdns([0,0,1,1,0,0,0,0])


else:
    exit

print(formatted)

Upvotes: 1

Views: 183

Answers (2)

Ski
Ski

Reputation: 31

I know this sounds simple, but where you are doing this:

print(formatted)

You should be doing this:

print(outputs)

Or iterating over your list and printing each one individually.

for line in outputs:
  print(line)

Upvotes: 1

user2988908
user2988908

Reputation:

absolutely.

First, you're printing formatted, not your outputs

Then to print all of the outputs on seperate lines:

print('\n'.join(outputs))

What does that do? str.join takes an iterable collection (a list) and puts the string in-between them. so 'X'.join(['a','b','c']) results in 'aXbXc'

Upvotes: 1

Related Questions