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.
hobby-programmer.com
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
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 183 Nov-29-2019, 06:19 AM
Last Post: ThomasL
  convert integers to a string tantony 1 196 Oct-04-2019, 06:40 PM
Last Post: ichabod801
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 275 Jul-25-2019, 11:32 PM
Last Post: scidam
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 378 Jul-01-2019, 03:06 AM
Last Post: metulburr
  ValueError: could not convert string to float michaelbs007 1 2,301 Jun-08-2019, 08:44 PM
Last Post: micseydel
  convert a string into an int in Python anjita 2 886 Jan-18-2019, 10:37 AM
Last Post: sandeepatel
  convert non-string with explicit base jacklee26 5 2,778 Nov-06-2018, 06:50 AM
Last Post: jacklee26
  Unable to print the exact Float values when I convert LIST to Sequence of Tuples? preethamalluri 1 617 Jul-12-2018, 09:03 AM
Last Post: buran
  Convert .csv string into float YOYOYO 1 2,347 Apr-25-2018, 12:15 PM
Last Post: buran
  Convert "%" str to float Andrsays1 2 1,106 Apr-10-2018, 04:04 AM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)