Python programming indentation issue -
i wonder if read code , tell me why happening. can't first 1 come across this, have looked around , can't find reply in book or elsewhere.
this has minor, can't see it.
# programme find , calculte radius # area , circumference of circle. def main(): print('radius\tarea\tcircumference') print('----------------------------') print() radius in range(1, 11): area in range(1, 11): 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:
class="lang-none prettyprint-override">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 aligned in first 2 columns, 4 out of 10 in 3rd column seem tabbed right. ??
to ensure alignment, can first create each number string of fixed width.
def main(): print('radius\tarea\tcircumference') print('----------------------------') print() radius in range(1, 11): area in range(1, 11): 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 python
No comments:
Post a Comment