SY_13 SY_13 - 5 months ago 10
Python Question

Explicit float conversion in python gives ParseError

I'm learning python in Coursera, I tried the assignment for the course but am not getting the desired result. This program is supposed to extract '0.8475' and convert it to float before printing it.

text = "X-DSPAM-Confidence: 0.8475";
pos=text.find('0');
s=text[pos:len(text)];
p=0.0;
p=(float)s;
print p;


Everytime I run this code, I get a
ParseError: bad input on line 5
.

What am I doing wrong?

Answer

As @ZdaR mentions, the call to the float function must pass the parameter inside the parentheses as p = float(s). I tested this in Python IDLE and the program worked correctly.

Also note that you should not end lines with ; in Python. The whitespace itself will handle that for you when you start a new line.