Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 ValueError: could not convert string to float: . BoaCoder3 Programmer named Tim Posts: 9 Threads: 6 Joined: Nov 2017 Reputation: 0 Likes received: 0 #1 Jan-03-2018, 12:24 PM 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 Windspar Minister of Silly Walks Posts: 498 Threads: 13 Joined: Oct 2016 Reputation: 36 Likes received: 90 #2 Jan-03-2018, 12:34 PM Python does recognize '.' as float. You need to print ratio. To see your error. 99 percent of computer problems exists between chair and keyboard. buran Posts: 6,401 Threads: 105 Joined: Sep 2016 Reputation: 499 Likes received: 1336 #3 Jan-03-2018, 01:01 PM (This post was last modified: Jan-03-2018, 01:01 PM by buran. Edited 1 time in total.) 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) >>>``` atlass218 Lumberjack Posts: 125 Threads: 28 Joined: Aug 2018 Reputation: 0 Likes received: 0 #4 Aug-24-2019, 06:26 AM (This post was last modified: Aug-24-2019, 06:27 AM by atlass218. Edited 1 time in total.) perhaps you want to write this sample code like that : ```ratio = list(raw_input('ratio numbers: ').replace(' ', '.')) ``` « Next Oldest | Next Newest »

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)