微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError: 'float' 对象没有属性 'strip'

如何解决AttributeError: 'float' 对象没有属性 'strip'

所以,我想了解为什么 float 函数不与 strip 方法混合。 我删除了 .strip() 并且它有效。我最初添加了它,以防用户在价格上增加空间。

有人能解释一下这个错误信息吗?

出现错误消息的位置:

user_update_price = float(input(f"\nWhat is the price of {user_update_input}: ")).strip()

完整代码

def product_update(product_list):
    core.function_clear()
    product_view(product_list)

    user_update_id = int(input("\nEnter 0 to cancel. Please enter an ID to update the product: "))
    if user_update_id == 0: 
        return
    user_update_input = input("\nEnter 0 to cancel. What is the new product's name? ").strip()
    if user_update_input.strip() == "0":
        return

    current_product = search(user_update_id,product_list)
    user_update_price = float(input(f"\nWhat is the price of {user_update_input}: ")).strip()
    print("\n")

    if user_update_input == '':
        user_update_input = current_product["product_name"]
    if user_update_price == '':
        user_update_price = current_product["price"]

    update_sql = "UPDATE products SET product_name = %s,price = %s WHERE product_id = %s"
    update_value = (user_update_input.title(),user_update_price,user_update_id)
    connectdb.cursor.execute(update_sql,update_value)
    connectdb.connection.commit()

解决方法

float 数据类型不支持 .strip() 因为该函数只能用于字符串。不要在转换为浮点数后应用 .strip(),而是将其应用到输入字符串本身。

试试:

user_update_price = float(input(f"\nWhat is the price of {user_update_input}: ").strip())

这应该可以修复错误。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?