Member Avatar for Atomicquasar

In my python class, we are using python 3.4.1 And we are typing in the programs from the book. Well, I've typed it in correctly and the output still doesn't align the decimal points. Can anyone help? It would be greatly appreciated. Here's the program code:

# This program displays the following
# floating-point numbers in a column
# with their decimal points aligned.
num1 = 127.899
num2 = 3456.148
num3 = 3.776
num4 = 264.821
num5 = 88.081
num6 = 799.999

# Display each number in a field of 7 spaces
# with 2 decimal places.
print(format(num1, '7.2f'))
print(format(num2, '7.2f'))
print(format(num3, '7.2f'))
print(format(num4, '7.2f'))
print(format(num5, '7.2f'))
print(format(num6, '7.2f'))

#EOF

Thanks!

Recommended Answers

All 5 Replies

I tried your code, and apparently it works: the decimal points are aligned. Can you post your output ?

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num1 = 127.899
>>> num2 = 3456.148
>>> num3 = 3.776
>>> num4 = 264.821
>>> num5 = 88.081
>>> num6 = 799.999
>>> print(format(num1, '7.2f'))
 127.90
>>> print(format(num2, '7.2f'))
3456.15
>>> print(format(num3, '7.2f'))
   3.78
>>> print(format(num4, '7.2f'))
 264.82
>>> print(format(num5, '7.2f'))
  88.08
>>> print(format(num6, '7.2f'))
 800.00
>>> 

"Aligning" depends also on the font used. If you are displaying the result in the console, then the console must use a fixed witdth font. If you want to experiment, save the result to a file, open the file with any text/word processor and change the font from proportional to fixed width.

commented: very good! +14
Member Avatar for Atomicquasar

Here is my output:

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

================================ RESTART ================================

127.90
3456.15
3.78
264.82
88.08
800.00

Ok, since you have it showing decimal point aligned, what font and size do you have it set to?

thanks.

Member Avatar for Atomicquasar

Thank you, Gribouillis and woooee! I had my font on arial and try calibri, neither worked to correct the problem. But when I copied my output to notepad, and looked at the font setting, it hit me can a rockslide. I needed to set it to font Consolas

Now, the decimal point are perfectly aligned. Thank you for your help, helping this old school beginning programmer.

I could not reproduce the bug. Here is my idle3 screen:

Oh, I see that you solved the issue. Great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.