Python Forum
string to float conversion fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string to float conversion fails
#1
Python version: 3.12
Windows 10, x64

This the problematic raw:

---------------------------------------------------------------
a = float(input("Vnesi ja osnovata a = "))
---------------------------------------------------------------

When I press "enter" I get this message:

Traceback (most recent call last):
File "C:\Users\5ar5r\Desktop\keops.py", line 1,
in a = float(input("Vnesi ja osnovata (a) = "))
ValueError: could not convert string to float: ''

I try to get a number value from the input and when I press "enter" I expect that Python will convert empty string into number 0, but it does not. Please, can you explain to me where the problem is?
Reply
#2
float("0") will return 0 because "0" is a numeric string representing the number 0. "" is not "0", and "" is not a string that represents any number. float(str) and int(str) raise a value error when the str cannot be interpreted as a numeric string.
Reply
#3
(Feb-21-2024, 05:12 PM)deanhystad Wrote: float("0") will return 0 because "0" is a numeric string representing the number 0. "" is not "0", and "" is not a string that represents any number. float(str) and int(str) raise a value error when the str cannot be interpreted as a numeric string.

So, any suggestion how to solve this?
I would like user to press "enter" to go to the next question if he doesn't want to input a valid number.
Reply
#4
(Feb-21-2024, 04:56 PM)PetarPetrenko Wrote: a = float(input("Vnesi ja osnovata a = "))
This question/answer excapes me.
When I run the above code, I get exactly what is intended.
enter 17, you get 17.0.
?
Paul
(win 11, python 3.11.2, Idle)
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
I guess the desired behavior is if user enters nothing, the program should return 0 instead of raising an exception.

This is not what float(str) does. Float raises an exception when the str is not a valid numeric string. The program must expect this will happen, so it needs to either check the string before calling float() or catch the exception. I would catch the exception.
Reply
#6
(Feb-21-2024, 06:34 PM)deanhystad Wrote: I would catch the exception.
No doubt!
P.
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#7
(Feb-21-2024, 06:21 PM)DPaul Wrote:
(Feb-21-2024, 04:56 PM)PetarPetrenko Wrote: a = float(input("Vnesi ja osnovata a = "))
This question/answer excapes me.
When I run the above code, I get exactly what is intended.
enter 17, you get 17.0.
?
Paul
(win 11, python 3.11.2, Idle)

If you press "enter" without any number, you will get an error.
Reply
#8
(Feb-22-2024, 11:09 AM)PetarPetrenko Wrote: If you press "enter" without any number, you will get an error.
That is what deanHystad has explained, and he proposed a solution.
P.
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#9
Try this, I think it catches all exceptions let me know if not!

def foolproof():
    print('Enter a number like 123 or a decimal like 1.23 or .12  or -1.2 Use . not comma , ')
    a = input("Vnesi ja osnovata a = ")
    if a == '':
        a = '0'
    # try to deal with negative numbers
    while '-' in a and a[0] != '-':
        a = input("If the number is negative, put - first. Enter a number ... ")
    # this should catch everything that is not a digit after replacing . and -
    while not a.replace('.', '').replace('-', '').replace(' ', '').isdigit():
        print('Enter a number like 123 or a decimal like 1.23 or .12  or -1.2... ')
        a = input("Vnesi ja osnovata a = ")
    # maybe user put - space  in front then the number? Get rid of space
    b = a.replace(' ', '')
    return float(b)
Just enter .123:

Output:
foolproof() Enter a number like 123 or a decimal like 1.23 or .12 or -1.2... Vnesi ja osnovata a = .123 0.123
Just press enter:

Output:
foolproof() Enter a number like 123 or a decimal like 1.23 or .12 or -1.2 Use . not comma , Vnesi ja osnovata a = 0.0
Use minus then space then number:

Output:
foolproof() Enter a number like 123 or a decimal like 1.23 or .12 or -1.2 Use . not comma , Vnesi ja osnovata a = www Enter a number like 123 or a decimal like 1.23 or .12 or -1.2... Vnesi ja osnovata a = - 5.4321 -5.4321
Reply
#10
I found a solution:

a = float(input("a = ") or 0)

It returns 0 when "enter" is pressed.

Thanks to everyone involved to help. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error could not convert string to float: deadendstreet 4 5,401 Oct-02-2019, 05:49 PM
Last Post: ichabod801
  ValueError: could not convert string to float: 'Pencil' Balakay97 3 5,921 Mar-08-2018, 07:30 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020