Python Forum
ValueError: could not convert string to float: .
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: could not convert string to float: .
#1
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
Reply
#2
Python does recognize '.' as float.
You need to print ratio. To see your error.
99 percent of computer problems exists between chair and keyboard.
Reply
#3
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'>
>>>
Reply
#4
perhaps you want to write this sample code like that :

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert each element of a list to a string for processing tester_V 6 337 Jun-16-2021, 02:11 AM
Last Post: tester_V
Question convert unlabeled list of tuples to json (string) masterAndreas 4 584 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert String of an int array to a Numpy array of ints mdsousa 5 651 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  Convert string to JSON using a for loop PG_Breizh 3 552 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  Convert hex value into string Sancho_Pansa 8 818 Dec-23-2020, 08:54 AM
Last Post: Sancho_Pansa
  convert string into multiple columns in python3 VAN 2 766 Sep-26-2020, 11:14 PM
Last Post: scidam
  How to convert a string "<... object at POINTER>" to an object? mandaxyz 5 945 Aug-08-2020, 10:44 PM
Last Post: mandaxyz
  ValueError: could not convert string to float: RandomCoder 3 1,647 Jul-27-2020, 07:38 AM
Last Post: ndc85430
  Why int() cannot pass a string representation of a float into int? majorjohnusa 1 580 Jul-09-2020, 05:26 AM
Last Post: Knight18
  could not convert string to float player1681 4 2,444 May-22-2020, 12:10 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020