如何解决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 举报,一经查实,本站将立刻删除。