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