Python Forum
ValueError: could not convert string to float: . - 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: ValueError: could not convert string to float: . (/thread-7296.html)



ValueError: could not convert string to float: . - BoaCoder3 - Jan-03-2018

In this programme i'm trying to solve a mathematical ratio problem, then calculate the squareroot, however, whenever i try to give it input like this: 2.5, it throws out the following error:
Error:
ValueError: could not convert string to float: .
, obviously it doesn't recognise the "." as a number. Here's the complete code:
# !/usr/bin/python
# -*- coding: utf8 -*-

import math


def data_processing():
    
    ratio = list(raw_input('ratio numbers: ').replace(' ', ''))

    r1 = float(ratio[0]) * float(ratio[1])
    r2 = float(ratio[2]) * float(ratio[3])

    a = r1/r2

    print math.sqrt(a)


data_processing()
How can i get the float() function to recognise a number like this: 2.5 as a floating point number with a decimal point instead of a group of numbers with the string "."? what is the easiest way to fix this? thanks


RE: ValueError: could not convert string to float: . - Windspar - Jan-03-2018

Python does recognize '.' as float.
You need to print ratio. To see your error.


RE: ValueError: could not convert string to float: . - buran - Jan-03-2018

what is expect user input at this line
ratio = list(raw_input('ratio numbers: ').replace(' ', ''))
>>> var1 = '2.5'
>>> var2 = float(var1)
>>> var2
2.5
>>> type(var2)
<type 'float'>
>>>



RE: ValueError: could not convert string to float: . - atlass218 - Aug-24-2019

perhaps you want to write this sample code like that :

ratio = list(raw_input('ratio numbers: ').replace(' ', '.'))