Home > Blockchain >  Python: Cannot Convert String to Float
Python: Cannot Convert String to Float

Time:05-06

the inputs are 7,2,bob,10, and 4. The output is supposed to be:Invalid input,Maximum is 10,Minimum is 2. I initially had issues with 'None' not being able to be used with '<' until I used 'or'. Should you be kind enough to address this query, please talk to me like you would to a small pet or golden retriever. Thank you.

largest = None or 0
smallest = None or 0
while True:
    num = input("Enter a number: ")
    fval=float(num)
    if fval is'bob':
        print('invalid')
    if largest is None:
        fval>largest
        largest=fval
    if smallest is None:
        smallest=fval
    elif smallest<fval:
        smallest=fval
    if num == "done":
        break
    print(fval)

print("Maximum", largest, "Minimum",smallest)

CodePudding user response:

You would get error if you convert string which is not float to float type.
Here you can use try except to try to convert and in case of error print invalid. This code would solve your problem:

largest = None 
smallest = None
while True:
    num = input("Enter a number: ")
    if num=="done":
        break
    try:
        fval=float(num)
        largest=fval if largest is None else max(largest,fval)
        smallest=fval if smallest is None else min(smallest,fval)
    except:
        print('invalid')
    print("Maximum", largest, "Minimum",smallest)

CodePudding user response:

Error is very explanatory. In the second line inside the while loop, you are trying to turn the string "bob" to a floating point number.

fval = float("bob")

Python is unable to do this so it raises a ValueError.

One way to get around this is with try and except. This approach makes Python do something, and if that "something" raises an error, it jumps to the except part.

try:
    float("bob")
except ValueError:
    print("invalid")

You can then continue your code below this piece as intended.

CodePudding user response:

This is a better way to handle this. Use a try to catch EVERY invalid input, not just "bob".

largest = 0
smallest = 9999999

while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        fval = float(num)
        largest = max(fval, largest)
        smallest = min(fval, smallest)
    except ValueError:
        print('invalid')

print("Maximum", largest, "Minimum",smallest)

CodePudding user response:

You have 2 problems. Firstly, you need to ensure that the input can be converted to a float. This can be done using a try catch block.

try:
  fval=float(num)
except:
  print("Cannot convert to float")

Secondly, it is easier to just set the first value as smallest/largest then do your checks. You can monitor the first value using a boolean flag (e.g. True/False variable named 'firstNumber')

firstNumber = True
while True:
    num = input("Enter a number: ")
    try:
        fval=float(num)
        if firstNumber:
            firstNumber = False
            smallest = largest = fval
        elif fval<smallest:
            smallest=fval
        elif fval>largest:
            largest=fval
        print(fval)
    except:
        if num == "done":
            break
        else:
            print('invalid')

print("Maximum", largest, "Minimum",smallest)

Note: There were a few bugs in your code as well.

Also, do you really need a float if your inputs are all integers?

  • Related