我想编写一个代码来创建一个
while True
循环并要求用户从显示的杂货列表中选择一个或多个项目。当用户完成后,我想跳出循环,计算这些商品的总价并将其打印到屏幕上。
import sqlite3
connection = sqlite3.connect('seasonalProduce.db')
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS productList (
productID integer PRIMARY KEY,
productName text NOT NULL,
productPrice real
);""")
#add rows (records) to the productList table
productRecords = [(1, "Apples", 4.59),
(2, "Grapes", 5.99),
(3, "Pumpkin", 3.00),
(4, "Strawberries", 2.50),
(5, "Leafy Greens", 4.00)]
cursor.executemany("INSERT OR IGNORE INTO productList (productID, productName, productPrice) VALUES(?,?,?);", productRecords)
connection.commit()
cursor.execute("SELECT * FROM productList;")
produce = cursor.fetchall()
for rows in produce:
print(rows)
connection.close()
while True:
while True:
try:
choice1 = input("Please enter the product ID number:")
if choice1 in range(8):
print("Thank you. Please enter the price of the item.")
price1 = input(float("price per kg:"))
break
else:
print("Please try again. Enter a number within range 1 to 7.")
except ValueError:
print("Invalid input. Please try again.")
while True:
choice2 = input("Please enter the product ID number:")
if choice2 in range(8):
print("Thank you.Please enter the price of the item.")
price1 = input(float("price per kg:"))
price2 = input(float("price per kg:"))
break
else:
print("Please try again. Enter a number within range 1 to 7.")
try:
userContinue = input("Would you like to add another item to your purchase? Y/N: ")
if userContinue.upper() == "Y":
choice3 = input("Please enter the product ID number:")
elif userContinue.upper() == "N":
total = price1 + price2
print("Your total is", total)
except ValueError:
print("Invalid input. Please try again.")`
代码中有一些错误。当你执行迭代时,有很多缩进错误。 while 和 try 语句以及缩进错误导致了程序中的大量冲突。因此必须将其删除。另一个问题是在选择输入之后的 if 语句中。在继续之前,我们必须将字符串转换为整数。分别从字符串转换重量。最后,如果用户输入 3 个产品,代码不会添加并显示总和。这是更正所有问题后的代码,它应该可以顺利运行:
import sqlite3
connection = sqlite3.connect('seasonalProduce.db')
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS productList (
productID integer PRIMARY KEY,
productName text NOT NULL,
productPrice real
);""")
#add rows (records) to the productList table
productRecords = [(1, "Apples", 4.59),
(2, "Grapes", 5.99),
(3, "Pumpkin", 3.00),
(4, "Strawberries", 2.50),
(5, "Leafy Greens", 4.00)]
cursor.executemany("INSERT OR IGNORE INTO productList (productID, productName, productPrice) VALUES(?,?,?);", productRecords)
connection.commit()
cursor.execute("SELECT * FROM productList;")
produce = cursor.fetchall()
for rows in produce:
print(rows)
connection.close()
choice1 = input("Please enter the product ID number:")
if int(choice1) in range(8):
print("Thank you. Please enter the price of the item.")
price1 = input("price per kg:")
float_price1=float(price1)
else:
print("Please try again. Enter a number within range 1 to 7.")
choice2 = input("Please enter the product ID number:")
if int(choice2) in range(8):
print("Thank you.Please enter the price of the item.")
price2 = input("price per kg:")
float_price2=float(price2)
else:
print("Please try again. Enter a number within range 1 to 7.")
userContinue = input("Would you like to add another item to your purchase? Y/N: ")
if userContinue.upper() == "Y":
choice3 = input("Please enter the product ID number:")
if int(choice2) in range(8):
print("Thank you.Please enter the price of the item.")
price3 = input("price per kg:")
float_price3=float(price3)
print("Your total is", float_price1+float_price2+float_price3)
elif userContinue.upper() == "N":
total = price1 + price2
print("Your total is", total)