print floating point number - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: print floating point number (/thread-4390.html) Pages:
1
2
|
print floating point number - Regulus - Aug-12-2017 my question here I use python 3 and trying to print a floating point number. But python is giving me the number to only one decimal place. What do I need to do to get a number to at least 6 decimal places? c_n = float(0) n = float(0.0) for n in range(300, 500): c_n = float(((12*n + 5)/(3*n + 18))) print ("n = %s and c_n = %s " % (n, (float(c_n)))) RE: print floating point number - Larz60+ - Aug-12-2017 Here's a portion of what I get without any changes: A better way to print would be:print('n = {} and c = {}'.format(n, float(c_n))) RE: print floating point number - Regulus - Aug-12-2017 Could the problem be my python environment? I code in Notepad++ and run the program at a DOS prompt. Here are the last few lines of my output with the new print command you kindly provided: n = 496 and c = 3.0 n = 497 and c = 3.0 n = 498 and c = 3.0 n = 499 and c = 3.0 RE: print floating point number - snippsat - Aug-12-2017 (Aug-12-2017, 10:29 PM)Regulus Wrote: I use python 3Do you? Run this. import sys print(sys.version)Add a dot 18. and it magically work.
RE: print floating point number - Regulus - Aug-13-2017 Well this is embarrassing. I installed Python 3.4 long ago and added its path to the Path Environment variable so I thought that was the version that was being used. When the program printed 2.7.5 as the python version I copied 3.4 to Desktop and saved the program in the folder with python.exe. Still it says it's running 2.7.5. Is that what's causing the floating point numbers to print incorrectly? How can I start running 3.4? I have been running python programs from a folder in Desktop. Btw, I don't understand what this suggestion means: "Add a dot 18. and it magically work." RE: print floating point number - hbknjr - Aug-13-2017 (Aug-13-2017, 05:40 AM)Regulus Wrote: I copied 3.4 to Desktop and saved the program in the folder with python.exe. Still it says it's running 2.7.5. In cmd use the full path to the python 3.4 exe. C:\Users\path to\python3\python.exe yourpythonfile.pyYou can also rename python.exe to python3.exe and add to your path, then you can simply type python3 to use python3.4 and python to use python2.7 RE: print floating point number - snippsat - Aug-13-2017 Regulus Wrote:Is that what's causing the floating point numbers to print incorrectly? How can I start running 3.4?Yes,and install 3.6 Python 3.6 and pip installation under Windows also part 2. (Aug-13-2017, 05:40 AM)Regulus Wrote: Btw, I don't understand what this suggestion means: "Add a dot 18. and it magically work."You make it float bye adding . ,then it work for Python 2.c_n = float(((12*n + 5)/(3*n + 18.))) >>> n = 18 >>> type(n) <class 'int'> >>> n = 18. >>> n 18.0 >>> type(n) <class 'float'> RE: print floating point number - Regulus - Aug-13-2017 Quote:hbkmjr wrote: In cmd use the full path to the python 3.4 exe.This works! But going to >>> by entering 'python3', then entering the program name, results in "File <stdin> , line 1 in <module> NameError: name: 'seq' is not defined" error. The program filename is seq.py and that's what I enter at >>>. It is in the Python34 directory where I am when I enter 'python3'. (Aug-13-2017, 09:42 AM)snippsat Wrote:That worked in 2.7.5! How strange.. Thank you.Regulus Wrote:Is that what's causing the floating point numbers to print incorrectly? How can I start running 3.4?Yes,and install 3.6 Python 3.6 and pip installation under Windows also part 2. RE: print floating point number - hbknjr - Aug-13-2017 (Aug-13-2017, 03:42 PM)Regulus Wrote: But going to >>> by entering 'python3', then entering the program name, results in "File <stdin> , line 1 in <module> NameError: name: 'seq' is not defined" error. The program filename is seq.py and that's what I enter at >>>. It is in the Python34 directory where I am when I enter 'python3'. Ok, so you opened python interpreter and typed the filename that's bound to get you name error. As Interpreter is to execute your code on the fly. To execute a .py file you have to pass python file as the argument to python.exe. If you correctly added python34 to the path and changed the python.exe name to python3.exe, You should be able to execute your seq.py file by typing following in CMD. python3 seq.py RE: print floating point number - snippsat - Aug-13-2017 (Aug-13-2017, 03:42 PM)Regulus Wrote: But going to >>> by entering 'python3', then entering the program name, results in "FileYou can not enter python3 in interactive shell.python3 is also for Linux command. Some advice follow my link over and install Python 3.6. Restart Pc. Now start cmd ,if all work it look like this.Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. Med enerett. C:\Windows\System32>cd\ C:\>python Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exit() C:\>pip -V pip 9.0.1 from c:\python36\lib\site-packages (python 3.6)Now is 3.6 the main version and pip work which is important.Can still use other version on OS,on Linux is python3 Windows can use py -version shortcut.C:\>py -3.4 Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exit() C:\>py -2.7 Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exit()Even install with pip work fine with py shortcut.C:\>py -3.4 -m pip install requests Collecting requests Downloading requests-2.18.3-py2.py3-none-any.whl (88kB) 100% |################################| 92kB 1.2MB/s Collecting chardet<3.1.0,>=3.0.2 (from requests) Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% |################################| 143kB 1.7MB/s Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.5 requests-2.18.3 urllib3-1.22 |