1

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. ??

4
  • How are you running it and on what OS? Commented Jun 23, 2014 at 2:10
  • I am running on Windows 8.1 and I am using the Python Interpreter. Commented Jun 23, 2014 at 2:12
  • why does u used. for lop for circumfrence? is that needed Commented Jun 23, 2014 at 2:17
  • There's nothing wrong with the code, that's just how \t works. Commented Jun 29, 2014 at 3:14

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

This works, but now only the circumference is formatted '.2f'. Can I make them all like that? I tried, but it did not work the way I tried it, but does work for sure.
I'm personally a fan of this style of formatting but be aware that this is not the new style python formatting you'll see in the other answers. You could refer to this style as "printf" formatting.
2

You need to give alignment to the output read string formatting a demo

 print('{:<10}{:<10}{:<10}'.format(area,radius,circumference))

Comments

1

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='')

1 Comment

Or maybe just specifying sep='\t' and getting rid of the explicit '\t' items.
0

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

Comments

0

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'))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.