Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 print floating point number
#1
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))))
Quote
#2
Here's a portion of what I get without any changes:
Output:
n = 300 and c_n = 3.9270152505446623 n = 301 and c_n = 3.9272529858849077 n = 302 and c_n = 3.9274891774891776 n = 303 and c_n = 3.9277238403451995 n = 304 and c_n = 3.9279569892473116 n = 305 and c_n = 3.9281886387995715 n = 306 and c_n = 3.9284188034188032 n = 307 and c_n = 3.928647497337593 n = 308 and c_n = 3.9288747346072186 n = 309 and c_n = 3.929100529100529
A better way to print would be:
    print('n = {} and c = {}'.format(n, float(c_n)))
Quote
#3
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
Quote
#4
(Aug-12-2017, 10:29 PM)Regulus Wrote: I use python 3
Do you?
Run this.
import sys

print(sys.version)
Add a dot 18. and it magically work.
Quote
#5
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."
Quote
#6
(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.py
You 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
Quote
#7
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'>
Quote
#8
Quote:hbkmjr wrote: In cmd use the full path to the python 3.4 exe.

C:\Users\path to\python3\python.exe yourpythonfile.py
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:
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.
Excellent advice. I will try it as soon as I resolve this problem I've had in Win 7 that has prevented me from installing anything from the internet since I downgraded from Win 10 to Win 7.
(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.)))
...
That worked in 2.7.5! How strange.. Thank you.
Quote
#9
(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
Quote
#10
(Aug-13-2017, 03:42 PM)Regulus Wrote: But going to >>> by entering 'python3', then entering the program name, results in "File
You 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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Print 'X' a number of times Than999 1 92 Jan-18-2020, 06:41 PM
Last Post: Larz60+
  connecting the first point to the last point Matplotlib omar_mohsen 0 88 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  how do I make a number only print once but the number keeps on decreasing? syafiq14 5 199 Jan-03-2020, 10:34 AM
Last Post: perfringo
  Complex floating issue arshad 2 429 Nov-05-2019, 03:26 PM
Last Post: arshad
  floating point arithmetic exDeveloper 2 221 Sep-25-2019, 04:33 PM
Last Post: DeaD_EyE
  finding the closest floating point number in a list Skaperen 17 872 Sep-19-2019, 10:39 PM
Last Post: Skaperen
  finding the next higher representable floating point value Skaperen 0 437 Sep-13-2019, 11:16 PM
Last Post: Skaperen
  print number of a list line per line lateublegende 2 519 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  Subtracting values between two dictionaries/ floating point numbers FloppyPoppy 5 1,345 Mar-04-2019, 01:00 PM
Last Post: snippsat
  How to get number with print statement Rehan11 1 693 Jul-28-2018, 02:01 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)