If your inputs are "sometimes" ints and "sometimes" floats then just wrap each input in a float(). You could make something more complex, but why would you?
Answer from CambridgeCro on Stack OverflowIf your inputs are "sometimes" ints and "sometimes" floats then just wrap each input in a float(). You could make something more complex, but why would you?
You could check for the presence of a decimal point in your string to decide if you want to coerce it into a float or an int.
number = input()
if '.' in number:
number = float(number)
else:
number = int(float(number))
Videos
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.
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)
Hi all, I have a string a = '1721244344.700249000', I want to convert it to a floating value.
Float() is returning only 2 places after decimal point. Like 1721244344.7
Is there a way I can convert the entire string to a floating point value and get all the decimal (upto 9 places after decimal point)?
I have to use python v2.7 for this.
Edit: I do not have problem in printing the all 9 decimal places but I need the floating value so that I can subtract another value so that I get the difference with accuracy upto 9 th decimal point.
Without using int() or float(), how do I convert string to its actual numerical value? Thanks!
Edit: Question below "Write a program that reads in a floating number less than 1 as a string. Without using the int() or float() functions, convert the string into its actual numerical value, and print the result of 1 - x, where x is the number."