简体   繁体   中英

I am getting an error : AttributeError: 'str' object has no attribute 'isfloat'

I am following a tuto for OOP regarding getters and setters, but I am getting an error:

AttributeError: 'str' object has no attribute 'isfloat'

@height.setter
def height(self, value):
    if value.isfloat():
        self.__height = value
    else:
        print("Please enter a number")

Does anyone know why this occurs ? Thank you in advance

You can use

@height.setter
def height(self, value):
    if isinstance(value, float): # Idea by Siva Shanmugam
        self.__height = value
    else:
        print('Please enter a number')

to test if value is a float, or you simply

@height.setter
def height(self, value):
    self.__height = float(value)

to get a TypeError if value can't convert value into float . Using this, int input and str with float character won't cause problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM