Bottom Page

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
Quote
#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.
Quote
#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'>
>>>
Quote
#4
perhaps you want to write this sample code like that :

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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to convert a string "<... object at POINTER>" to an object? mandaxyz 5 193 Aug-08-2020, 10:44 PM
Last Post: mandaxyz
  ValueError: could not convert string to float: RandomCoder 3 314 Jul-27-2020, 07:38 AM
Last Post: ndc85430
  Why int() cannot pass a string representation of a float into int? majorjohnusa 1 137 Jul-09-2020, 05:26 AM
Last Post: Knight18
  could not convert string to float player1681 4 1,029 May-22-2020, 12:10 PM
Last Post: buran
  How do I convert this string back to a list of integers? donmerch 6 558 Apr-05-2020, 06:43 PM
Last Post: donmerch
  could not convert string to float: C JamesW 2 1,052 Jan-29-2020, 04:56 PM
Last Post: JamesW
  Python convert multi line into single line formatted string karthidec 2 1,006 Dec-23-2019, 12:46 PM
Last Post: karthidec
  How to check if user entered string or integer or float?? prateek3 5 5,602 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  how to convert list to string after append !! evilcode1 3 455 Dec-16-2019, 09:50 AM
Last Post: buran
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 973 Nov-29-2019, 06:19 AM
Last Post: ThomasL

Forum Jump:


Users browsing this thread: 1 Guest(s)