Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: could not convert string to float: '8/3'

I am having the folllowing instruction

x1,y1,x2,y2,x3,y3,xp,yp = map(float, input().split())

When I am trying to execute it for a fractional value, e.g. 8/3, I get an error message.

x1,y1,x2,y2,x3,y3,xp,yp = map(float, input().split())
0.1 0.2 -8 -16.67 0 0.1 8/3 1
# output
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    x1,y1,x2,y2,x3,y3,xp,yp = map(float, input().split())
ValueError: could not convert string to float: '8/3'

What am I doing wrong here? Thanks in advance for any kind of help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

8/3 is not a valid notation for a python float. However, you can parse these inputs using the fractions module:

>>> import fractions
>>> inp = '0.1 0.2 8/3 1/2 -5/10'
>>> [float(fractions.Fraction(s)) for s in inp.split()]
[0.1, 0.2, 2.6666666666666665, 0.5, -0.5]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading