【问题标题】:AttributeError: 'str' object has no attribute 'float' [duplicate]AttributeError:“str”对象没有属性“float”[重复]
【发布时间】:2021-02-14 10:23:26
【问题描述】:

错误消息最初出现的位置:

user_add_price = float(input(f"\nEnter 0 to cancel. What is the price of {user_add_product}? ").strip())

我删除了 float 函数,因为它无法转换为字符串。我想这样做,所以当我在行 (user_add_price) 询问他们时,用户可以添加他们想要的任何内容,如果他们不添加整数/浮点数(数字),则会出现此行:

print(f"\nInvalid input {user_add_price}, please try again.")
user_add_price = input(f"\nEnter 0 to cancel. What is the price of {user_add_product}? ").strip()

在下面显示的终端中,它应该是这样的。但是,上面的代码在我不想要的价格中的代码输出中有效:kldjksajd,因为它是一个字符串。

如何修复代码输出中的价格?

终端:

Enter 0 to cancel. What is the price of coconut? kldjksajd

Invalid input kldjksajd, please try again.

代码输出:

Product ID: 34, Product: coconut, Price: kldjksajd

代码输入:

def product_add(product_list):
    core.function_clear()
    product_view(product_list)
    user_add_product = input("\nEnter 0 to cancel. What product would you like to add? ").strip()

    if user_add_product == "0": # putting 0 does not work, it has to be "0"
        return

    user_add_price = input(f"\nEnter 0 to cancel. What is the price of {user_add_product}? ").strip()
    if user_add_price == "0":
        return

    else:
        print(f"\nInvalid input {user_add_price}, please try again.")

        add_sql = "INSERT INTO products (product_name, price) VALUES (%s, %s)"
        add_value = (user_add_product.title(), user_add_price) # add .title() here so it capitalises the word in the database
        connectdb.cursor.execute(add_sql, add_value)
        connectdb.connection.commit()

【问题讨论】:

    标签: python sql python-3.x


    【解决方案1】:

    关键是转换为浮点数以及如何处理。

    def product_add():
        # I REMOVE product_list FOR reproductibity in my machine
        # core.function_clear()
        # product_view(product_list)
        user_add_product = input("\nEnter 0 to cancel. What product would you like to add? ").strip()
    
        if user_add_product == "0": # putting 0 does not work, it has to be "0"
            return
        user_add_price = input(f"\nEnter 0 to cancel. What is the price of {user_add_product}? ").strip()
        # Can the input be converted into float?
        try:
            user_add_price = float(user_add_price)
        except:
            print(f"\nInvalid input {user_add_price}, please try again.")
    
        """Here the rest of the code, I comment for simplicty"""
        # add_sql = "INSERT INTO products (product_name, price) VALUES (%s, %s)"
        # add_value = (user_add_product.title(), user_add_price) # add .title() here so it capitalises the word in the database
        # connectdb.cursor.execute(add_sql, add_value)
        # connectdb.connection.commit()
    
    product_add()
    

    【讨论】:

      【解决方案2】:

      您可以尝试将输入转换为浮点数,因为 input() 方法总是返回一个字符串。

      else:
          try:
              user_add_price = float(user_add_price)
          except ValueError:
              print(f"\nInvalid input {user_add_price}, please try again.")
              return
      
          add_sql = "INSERT INTO products (product_name, price) VALUES (%s, %s)"
          add_value = (user_add_product.title(), user_add_price) # add .title() here so it capitalises the word in the database
          connectdb.cursor.execute(add_sql, add_value)
          connectdb.connection.commit()
      

      【讨论】:

        【解决方案3】:

        您可以使用异常处理来处理这种情况。捕获插入的无效价格,只有在没有异常发生时才将值插入到表中

        try:
            user_add_price = float(input(f"\nEnter 0 to cancel. What is the price of {user_add_product}? ").strip())
            add_sql = "INSERT INTO products (product_name, price) VALUES (%s, %s)"
            add_value = (user_add_product.title(), user_add_price) # add .title() here so it capitalises the word in the database
            connectdb.cursor.execute(add_sql, add_value)
            connectdb.connection.commit()
        except ValueError:
            print(f"\nInvalid input {user_add_price}, please try again.")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-13
          • 2017-07-02
          • 2016-04-15
          • 2020-01-11
          • 1970-01-01
          相关资源
          最近更新 更多