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 string to float in list jacklee26 6 1,815 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,714 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,237 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,389 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,279 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,421 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,230 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,395 Feb-13-2022, 04:50 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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