1

I am working on simple code to print variables from a for loop and I am stuck on the 4th line of the code. I get an invalid syntax error. Any help would be appreciated. My desired output is

print('-----------------------------------------')
print(output1)
print('-----------------------------------------')
print(output4)
print('-----------------------------------------')
print(output2)
print('-----------------------------------------')
print(output3)
print('-----------------------------------------')
print(output5)
print('-----------------------------------------')

-code below

printt=[1,4,2,3,5]
for numm in printt:
    print('-----------------------------------------')
    print(output'%d',%(numm))
mikerosd
  • 25
  • 6

4 Answers4

5

I think, the below would be helpful.

>>> printt=[1,4,2,3,5]
>>> for numm in printt:
...     print('-----------------------------------------')
...     print('output%d' % (numm))
...
-----------------------------------------
output1
-----------------------------------------
output4
-----------------------------------------
output2
-----------------------------------------
output3
-----------------------------------------
output5
>>>
hygull
  • 8,464
  • 2
  • 43
  • 52
  • I believe that this person has assigned values to `output1 = ...` et. al., and is attempting to output those values dynamically. – Patrick Haugh Aug 08 '18 at 13:30
1

The output variable is not defined in this case. You should change it output to 'output'.

print('output%d' % (numm))

or

print('output%d'.format(numm))
Rafał
  • 685
  • 6
  • 13
0

You can use this line instead:

 print('output{}').format(numm)
Joe
  • 12,057
  • 5
  • 39
  • 55
0

If your code has output1, output2, output3, output4 and output5 as variables. It would be better if you use a list. like:

_output_list = [output1, output4, output2, output3, output5]
for _out in _output_list:
    print('-----------------------------------------')
    print(_out)
Anil Kumar
  • 102
  • 1
  • 5