0

Where the error message initially appeared:

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

I removed the float function as it cannot be converted to a string. I wanted to do that, so the user can add whatever they want when I asked them at the line (user_add_price) and if they do not add integers/floats (numbers), this line would appear:

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()

In the terminal shown below, it appears as it should be. This code above works, however, in the code output in the Price I do not want: kldjksajd because it is a string.

How can I fix the Price in the code output?

Terminal:

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

Invalid input kldjksajd, please try again.

Code output:

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

Code input:

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()
francescowang
  • 413
  • 1
  • 6
  • 12

3 Answers3

1

The key is the conversion to float and how to handle that.

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()
Rafael Valero
  • 2,736
  • 18
  • 28
0

You can use exception handling to cover that case. Catch the invalid price being inserted and insert the value into the table only when no exception occurs

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.")
Sandeep Gusain
  • 127
  • 1
  • 1
  • 10
0

You could try to convert the input to a float, as the input() method always returns a string.

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()
Mar
  • 167
  • 1
  • 8