Reputation: 33
import math
#get base
inputOK = False
while not inputOK:
base = input('Enter base:')
if type(base) == type(1.0): inputOK = True
else: print('Enter, Base must be a floating point number.')
Enter base:1.0 Enter, Base must be a floating point number.
I can't get a correct answer when I input 1.0. It always output Base must be a floating point number. I want to get True and exit the loop. What's wrong with my program.
Upvotes: 0
Views: 36
Reputation: 10971
input
returns str
object so we need to convert to float manually,isinstance
for type checking (if it needed).Following EAFP we can write
# get base
inputOK = False
while not inputOK:
try:
# user can pass 'inf', 'nan', no error will be raised
# should we check this cases?
base = float(input('Enter base:'))
except ValueError:
print('Base must be an integer or floating point number.')
else:
inputOK = True
Upvotes: 1