I have a code that I am trying to convert but it’s written in Python 2 and I would like to print this code in Python 3. However, it’s not able to print in matrix format. I am getting output in an unrecognizable table format.
The code is following:
JavaScript
x
16
16
1
for n in cols:
2
print('/t',n),
3
print
4
cost = 0
5
6
for g in sorted(costs):
7
print('t', g)
8
for n in cols:
9
y = res[g][n]
10
if y != 0:
11
print (y),
12
cost += y * costs[g][n]
13
print ('t'),
14
print
15
print ("nnTotal Cost = ", cost)
16
The expected output is in the following format:
JavaScript
1
10
10
1
|0 |A |B |C |D |E |
2
|- |- |- |- |- |- |
3
|W | | |20| | |
4
|X | |40|10| |20|
5
|Y | |20| | |30|
6
|Z | | | | |60|
7
8
9
Total Cost = 1000
10
Could you suggest what changes I need to make in this code?
Advertisement
Answer
In py 2 print did not have parenthesis, in py3 is a must.
python3 simple print
JavaScript
1
3
1
# in py3 a print must be like
2
print("my text")
3
python3 print with no newline / carriage return
Also in your py2 code you have print ('t'),
please mind the comma after print => which means do not add a newline after print.
In python3 would translate to
JavaScript
1
2
1
print('t', end='')
2