Python 3.x 如何制作杂货收据程序
我对编程很陌生 我的问题是我的if语句得到了一个索引器。所以我把if语句放在另一个单元格中,用不同的列表尝试,结果很好。我做错了什么Python 3.x 如何制作杂货收据程序,python-3.x,function,Python 3.x,Function,我对编程很陌生 我的问题是我的if语句得到了一个索引器。所以我把if语句放在另一个单元格中,用不同的列表尝试,结果很好。我做错了什么 from datetime import datetime import math def GroceryReceipt(): grocery = [["bread", 3], ["eggs", 3], ["soda", 7], ["chips", 3], ["bee
from datetime import datetime
import math
def GroceryReceipt():
grocery = [["bread", 3], ["eggs", 3], ["soda", 7], ["chips", 3], ["beef", 8]]
amountGroceries = int(input("Input the amount of food you wish to buy: "))
userGroc = []
for i in range(amountGroceries):
buyGroceries = input("Pick some foods: ")
userGroc.append(buyGroceries)
for j in range(len(grocery)):
if(len(userGroc) < j):
break
else:
print("$",grocery[j][1])
if(userGroc[j] in grocery[j][0]):
add = grocery[j][1] + grocery[j][1]
tax = math.pi/100 * add
total = tax + add
round(total, 2)
x = datetime.today()
print(total, x)
GroceryReceipt()
从日期时间导入日期时间
输入数学
def GroceryReceipt():
杂货店=[[“面包”,3],“鸡蛋”,3],“苏打”,7],“薯条”,3],“牛肉”,8]]
amountGroceries=int(输入(“输入您希望购买的食物数量:”)
userGroc=[]
对于范围内的i(食品杂货数量):
buyGroceries=输入(“选择一些食物:”)
userGroc.append(购买杂货)
对于范围内的j(len(杂货店)):
如果(len(userGroc)
在杂货店=[[“面包”,3],“鸡蛋”,3],“苏打”,7],“薯条”,3],“牛肉”,8]
你忘了再加一个“]。如果您添加它,它应该可以正常工作。使用如下“else”命令:
from datetime import datetime
import math
def GroceryReceipt():
grocery = [["bread", 3], ["eggs", 3], ["soda", 7], ["chips", 3]], ["beef", 8]
amountGroceries = int(input("Input the amount of food you wish to buy: "))
userGroc = []
for i in range(amountGroceries):
buyGroceries = input("Pick some foods: ")
userGroc.append(buyGroceries)
for j in range(len(grocery)):
if(len(userGroc) < j):
break
else: # Use 'else' here
print("$3")
GroceryReceipt()
从日期时间导入日期时间
输入数学
def GroceryReceipt():
杂货店=[“面包”,3],“鸡蛋”,3],“苏打”,7],“薯条”,3],[“牛肉”,8]
amountGroceries=int(输入(“输入您希望购买的食物数量:”)
userGroc=[]
对于范围内的i(食品杂货数量):
buyGroceries=输入(“选择一些食物:”)
userGroc.append(购买杂货)
对于范围内的j(len(杂货店)):
如果(len(userGroc)
如果'if'语句的长度userGrocelse语句只有在“if”语句不满足要求时才会执行,否则两个“if”语句都会执行。作为程序员,一个巨大的挑战是如何将程序拆分,以降低代码中任何给定点的复杂性。另一个挑战是选择正确的数据结构以使任务更容易。这里有另一种方法来设想如何解决您的问题,并将这些问题考虑在内 例如:
from datetime import datetime
import math
in_stock = {
"bread": 3.0,
"eggs": 3.0,
"soda": 7.0,
"chips": 3.0,
"beef": 8.0
}
def select_stock_item():
stock_string = ", ".join([key for key in in_stock])
while True:
item = input(
f"Please choose an item from available stock\n[{stock_string}]: "
).casefold()
if not item in in_stock:
print(f"I'm sorry, that item is not in stock.")
continue
return item, in_stock[item]
def grocery_session():
purchase_qty = int(input("How many items will you be purchasing today? "))
purchases = []
for _ in range(purchase_qty):
purchases.append(select_stock_item())
for one_purchase in purchases:
purchase_subtotal = sum(one_purchase[1] for one_purchase in purchases)
tax_on_purchase = purchase_subtotal * math.pi / 100
purchase_total = purchase_subtotal + tax_on_purchase
date_and_time = datetime.today().strftime("%m/%d/%y %H:%M")
print("Thank you for your purchases.")
print(f"You spent a total of ${round(purchase_total, 2)} on {date_and_time}")
print("Please come again.")
grocery_session()
输出:
How many items will you be purchasing today? 3
Please choose an item from available stock
[bread, eggs, soda, chips, beef]: soap
I'm sorry, that item is not in stock.
Please choose an item from available stock
[bread, eggs, soda, chips, beef]: bread
Please choose an item from available stock
[bread, eggs, soda, chips, beef]: eggs
Please choose an item from available stock
[bread, eggs, soda, chips, beef]: soda
Thank you for your purchases.
You spent a total of $13.41 on 03/20/21 15:55
Please come again.
如果列表索引超出范围,则无法正常工作。请分享一个示例,说明根据用户的相应输入,您希望得到什么样的输出。很难说你们想用你们的
for j
循环来完成什么。我试图得到杂货店清单中物品旁边的整数,也就是它的价格。然后我想把总数加起来,用进口数学加上税,还想打印购买的日期和时间。