1

I wanted to know if there was a way to reproduce an output from one section of a code to the end of the code. So I am assuming I need to assign a variable to the print output function. Anyway this is part of my code that I want to store variable output to a variable and reproduce an output anywhere in my code:

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' print(output.format(busnum,busname,scaled_power)) 

1 Answer 1

4

You'll need to assign the result of output.format to a variable; not the value of the print function.

formatted_output = output.format(busnum, busname, scaled_power) 

The print function will always return None. In your loop, if you need the output for each iteration, store them in a list.

outputs = [] 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) print(formatted) 
Sign up to request clarification or add additional context in comments.

5 Comments

what is outputs[]
@mike: outputs is defined as an empty list. Elements are being added to the list with output.append(formatted) within the loop. Check out the documentation for list.
I receive error AttributeError: 'list' object has no attribute 'format'
@mike: I had typed output rather than outputs. I've made the correction in my answer.
Am I suppose to write this in the code I have right now. Also the goal was to output this statement at the end of my code. so I tried using code you gave me at the end of my code and it sill gives me same error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.