Reputation: 67
I wonder if some could read this code and tell me why this is happening. I can't be the first one to come across this, but I have looked around and I can't find an answer in a book or elsewhere.
This has to be something minor, but I can't see it.
# This program will find and calculte the radius
# area and circumference of a circle.
def main():
print('Radius\tArea\tCircumference')
print('----------------------------')
print()
for radius in range(1, 11):
for area in range(1, 11):
for circumference in range(1, 11):
pi = 3.14
diameter = radius * 2
radius = diameter / 2
area = pi * radius**2
circumference = (2 * pi) * radius
print(radius, '\t', area, '\t',format(circumference, '.2f'))
main()
Output:
Radius Area Circumference
----------------------------
1.0 3.14 6.28
2.0 12.56 12.56
3.0 28.26 18.84
4.0 50.24 25.12
5.0 78.5 31.40
6.0 113.04 37.68
7.0 153.86 43.96
8.0 200.96 50.24
9.0 254.34 56.52
10.0 314.0 62.80
>>>
The out-put is aligned in the first two columns, but four out of the ten in the third column seem to be tabbed to the right. ??
Upvotes: 1
Views: 275
Reputation: 550
i rearranged your code, now it works.
def main():
print('Radius\tArea\tCircumference')
print('----------------------------')
print()
for radius in range(1, 11):
for area in range(1, 11):
for circumference in range(1, 11):
pi = 3.14
diameter = radius * 2
radius = diameter / 2
area = pi * radius**2
circumference = (2 * pi) * radius
print(radius, '\t', format(area, '.2f')+'\t ' ,format(circumference, '.2f'))
Upvotes: 0
Reputation: 8692
You need to give alignment to the output read string formatting a demo
print('{:<10}{:<10}{:<10}'.format(area,radius,circumference))
Upvotes: 2
Reputation: 24039
To ensure alignment, you can first make each number a string of fixed width.
def main():
print('Radius\tArea\tCircumference')
print('----------------------------')
print()
for radius in range(1, 11):
for area in range(1, 11):
for circumference in range(1, 11):
pi = 3.14
diameter = radius * 2
radius = diameter / 2
area = pi * radius**2
circumference = (2 * pi) * radius
#print(radius, '\t', area, '\t' , format(circumference, ".2f"))
radius_str = "%0.2f" % radius
area_str = "%0.2f" % area
circumference_str = "%0.2f" % circumference
print("%6s\t%6s\t%6s" % (radius_str, area_str, circumference_str))
main()
Output:
Radius Area Circumference
----------------------------
1.00 3.14 6.28
2.00 12.56 12.56
3.00 28.26 18.84
4.00 50.24 25.12
5.00 78.50 31.40
6.00 113.04 37.68
7.00 153.86 43.96
8.00 200.96 50.24
9.00 254.34 56.52
10.00 314.00 62.80
Upvotes: 2
Reputation: 59571
You will want to use a string format to ensure you have consistent spacing between your columns:
line = '{:>12} {:>12} {:>12}'.format(radius, area, format(circumference, '.2f'))
print(line)
The value 12 means each of your columns (including the text in it) will be 12 characters wide.
Here's the official reference to String formatting in Python
Upvotes: 0
Reputation: 57460
This is a combination of the way that tab ('\t'
) works and the way that Python's print
works. By default, print
inserts a space between each item it prints out, and so what's actually being printed out on (for example) line 6 is:
6.0<SPACE><TAB><SPACE>113.04<SPACE><TAB><SPACE>37.68
Tab, on the other hand, advances to the next column that is a multiple of eight spaces from the start of the line. When the second tab on line 6 is printed, 8 characters have been output since the previous tab, and so the tab ends up printing out a full eight spaces to get to the next tab stop.
The easiest way to fix this is to eliminate the spaces between print
items by explicitly specifiying an empty sep
value:
print(radius, '\t', area, '\t',format(circumference, '.2f'), sep='')
Upvotes: 1