Python Forum
string to float conversion fails - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: string to float conversion fails (/thread-41644.html)

Pages: 1 2


string to float conversion fails - PetarPetrenko - Feb-21-2024

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?


RE: string to float conversion fails - deanhystad - Feb-21-2024

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.


RE: string to float conversion fails - PetarPetrenko - Feb-21-2024

(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.


RE: string to float conversion fails - DPaul - Feb-21-2024

(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)


RE: string to float conversion fails - deanhystad - Feb-21-2024

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.


RE: string to float conversion fails - DPaul - Feb-22-2024

(Feb-21-2024, 06:34 PM)deanhystad Wrote: I would catch the exception.
No doubt!
P.


RE: string to float conversion fails - PetarPetrenko - Feb-22-2024

(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.


RE: string to float conversion fails - DPaul - Feb-22-2024

(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.


RE: string to float conversion fails - Pedroski55 - Feb-23-2024

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



RE: string to float conversion fails - PetarPetrenko - Feb-29-2024

I found a solution:

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

It returns 0 when "enter" is pressed.

Thanks to everyone involved to help. Smile