1

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)
1
  • Why are you printing formatted instead of outputs? outputs is the list of all your formatted output strings. Commented Sep 6, 2017 at 19:47

2 Answers 2

1

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'

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

Comments

1

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)

3 Comments

just want to highlight that in Python 3 the parenthesis for print() is necessary
Thank you. Is there a way to print each statement on a new line rather than having each statement next to each other: For example:['Bus #: 126688\t Area Station: Buchanan\t New Load Total: 125.0 MW\t', 'Bus #: 126695\t Area Station: Millwood West\t New Load Total: 85.0 MW\t', 'Bus #: 126696\t Area Station: Ossining West\t New Load Total: 77.0 MW\t']
You may want to add a newline ('\n') to the beginning or end of each entry, too. @chowsai, Thanks for that. I am kind of used to 2.7 :P

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.