【发布时间】: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