user979153
user979153

Reputation: 31

Converting string to float with Python 3.2

The purpose of this program is to display the sum, average, max, and min of use based input.

count=0.0
Sum=0.0
average=0.0
data=float(input("Enter a number or just ENTER to quit:"))
Min=data
Max=data

while data!="":
    count+=1
    number=float(data)
    Sum+=number
    average=Sum/count

    if data<Min:
        Min=data
    if data>Max:
        Max=data
    data=float(input("Enter a number or just ENTER to quit:"))

print(count,("numbers entered."))
print("Sum:",Sum)
print("Average:",average)
print("Min:",Min)
print("Max:",Max)

The problem is with line 20: data=float(input("Enter a number or just ENTER to quit:"))

When i press ENTER to end the loop it says that it was unable to convert string to float and errors. What am I doing wrong ?????

Upvotes: 3

Views: 26998

Answers (5)

Javapocalypse
Javapocalypse

Reputation: 2363

By default, the data type of input is string. Pressing ENTER will return an empty string which cannot be casted into a float, since the string is empty and there is nothing to be casted, which is generating the error. Here are two solutions to handle this error.

Solution 1

Do not cast input into float directly, but cast it when assigning the value of input to any variable.

data=input("Enter a number or just ENTER to quit:")

Add the following statement before the loop to handle the condition when the user wants to exit without entering any number.

if not data: # If user wants to exit without entering any number
    Max = 0
    Min = 0
else:  # Cast the inputs by user to float
    Max = float(data)
    Min = float(data)

Lastly add the following line at the beginning inside the loop.

data = float(data)

Complete Code

count=0.0
Sum=0.0
average=0.0
data=input("Enter a number or just ENTER to quit:")
if not data:
    Max = 0
    Min = 0
else:
    Max = float(data)
    Min = float(data)

while data!='':
    data = float(data)
    count+=1 
    number=float(data)
    Sum+=number
    average=Sum/count

    if data<Min:
        Min=data
    if data>Max:
        Max=data
    data=input("Enter a number or just ENTER to quit:")

print(count,("numbers entered."))
print("Sum:",Sum)
print("Average:",average)
print("Min:",Min)
print("Max:",Max)

Solution 2

What you can also do is set a default value for empty inputs. Here since you are casting input into a float, you can set any number as the default value for empty inputs. However, I will not prefer this because if the user enters the same number as the default value, the loop will be terminated. In the following example I've only modified two lines of your code and set the default value for empty inputs as 0.

count=0.0
Sum=0.0
average=0.0
data=float(input("Enter a number or just ENTER to quit:") or 0)
Min=data
Max=data

while data!=0:
    count+=1
    number=float(data)
    Sum+=number
    average=Sum/count

    if data<Min:
        Min=data
    if data>Max:
        Max=data
    data=float(input("Enter a number or just ENTER to quit:") or 0)

print(count,("numbers entered."))
print("Sum:",Sum)
print("Average:",average)
print("Min:",Min)
print("Max:",Max)

Upvotes: 2

Hushen
Hushen

Reputation: 613

Python by default takes input in the string format only. Don't do direct type casting in python like string to float. It will give an error immediately. The best way to conversion of type string to float is::

string_data = input("enter the data")
enter the data 123
string_data '123'
float_data = float(string_data)
float_data 123.0

Upvotes: 0

TimRin
TimRin

Reputation: 77

Your variable names are incorrect, because sum, min, and max are all functions. Change them to different names such as "datasum", "datamin", and "datamax", so that the program does not confuse them with the functions.

Upvotes: 1

Chriszuma
Chriszuma

Reputation: 4557

The float() function raises an exception for blank inputs. You must catch this exception for your loop to work as intended. Here is the simplest fix:

In Python 2.x, it is actually the input() call that raises the exception, not float(). So, if you are using Python 2.x, my solution is the only one here that works.

while True:
    count+=1
    Sum+=data
    average=Sum/count

    if data<Min:
        Min=data
    if data>Max:
        Max=data
    try:
      data=float(input("Enter a number or just ENTER to quit:"))
    except:
      break

Upvotes: 1

Cat Plus Plus
Cat Plus Plus

Reputation: 129774

Well, you shouldn't convert to float immediately. Also, this is not how you do do..while loop in Python.

while True:
    data = input('Enter a number or ENTER to quit: ')
    if not data: break

    data = float(data)
    # ...

This way you don't have to duplicate code, or to prolong the life of data name unnecessarily.

Upvotes: 3

Related Questions