Wondering if there is a better way to do this. Python is my first programming language.
while True:
amount_per_month = raw_input('\nWhat is the monthly cost?\n==> ')
# seperate dollars and cents.
amount_list = amount_per_month.split(".")
# checking if int(str) is digit
if len(amount_list) == 1 and amount_list[0].isdigit() == True:
break
# checking if float(str) is digit
elif len(amount_list) == 2 and amount_list[0].isdigit() and amount_list[1].isdigit() == True:
break
else:
raw_input('Only enter digits 0 - 9. Press Enter to try again.')
continue
Just try to make it a
floatand handle the exception thrown if it can’t be converted.TypeErroris redundant here, but if the conversion was operating on other Python objects (not just strings), then it would be required to catch things likefloat( [1, 2, 3] ).