1

So... I have this primitive calculator that runs fine on my cellphone, but when I try to run it on Windows 10 I get...

ValueError: could not convert string to float

I don't know what the problem is, I've tried using raw_input but it doesn't work ether. Please keep in mind I'm green and am not aware of most methods for getting around a problem like this

num1 = float(input ()) #take a float and store it
chars = input () #take a string          and store it
num2 = float(input ())
6
  • 1
    you should not have characters in your input and don't use input use raw_input instead. Commented Aug 1, 2016 at 13:37
  • I don't intend to input characters. My code does not allow me to input even a float it simply reads the line, skips it and gives me a ValueError . I'll post the whole program once I get on my laptop. Commented Aug 1, 2016 at 13:40
  • Please include what inputs are failing. (What examples you're trying, if "everything".) Commented Aug 1, 2016 at 14:31
  • Give me a few minutes Commented Aug 1, 2016 at 14:38
  • I'm trying to send the whole program but it does not fit into the comments section. Commented Aug 1, 2016 at 15:01

2 Answers 2

2

your code only convert string that are integers like in below statement

num1 = float(input ()) #take a float and store it ex 13
print num1 # output 13.0

if you provide 13 as a input it will give the output as 13.0 but if you provide SOMEONEE as input it will give ValueError

And it is same with the case of raw_input() but the difference is that by default raw_input() takes input as a string and input() takes input as what is provided to the function

Sign up to request clarification or add additional context in comments.

3 Comments

I am aware that inputting strings gives a ValueError. My program does not even allow me to input onto the variable, it simply skips that command. BUT I have the exact same code running a calculator on my phone and it works but it won't work on my desktop
Please edit the question and explain in brief what you want?
0

I think this is happening because in some cases 'input' contains non-numerical characters. Python is smart and when a string only contains numbers, it can be converted from string to float. When the string contains non-numerical characters, it is not possible for Python to convert it to a float.

You could fix this a few ways:

  1. Find out why and when there are non-numerical characters in your input and then fix it.
  2. Check if input contains numbers only with: isdecimal()
  3. Use a try/except

isdecimal() example:

my_input = raw_input()
if my_input.isdecimal():
    print("Ok, go ahead its all numbers")

UPDATE: Two-Bit-Alchemist had some great advice in the comments, so I put it in my answer.

7 Comments

The isdigit method is less than great here because (1.) anything with a decimal point will fail this test (alternative: isdecimal) and (2.) the OP needs numbers to calculate with later so may as well go ahead and cast, being prepared to catch ValueError (your option 3).
@Two-BitAlchemist thanks for the pointers! I edited my answer :)
Your heading still says "isdigit" example. I tried to fix it but your latest edit you accidentally rolled it back ;)
FIXED! :) Thanks again
Anything with a decimal point will also fail isdecimal(). The difference between isdigit and isdecimal is apparently just some unicode characters.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.