Python 查找小计的总数
我正在运行一个程序,它会提示用户在“快速”结账行输入项目数。然后,它向用户请求商品的价格和数量,并打印小计。一旦用户输入的所有项目均已入账,程序将显示所有小计的总数。我已经到了最后一部分,我需要把用户的小计加起来。任何帮助都将不胜感激Python 查找小计的总数,python,python-3.x,Python,Python 3.x,我正在运行一个程序,它会提示用户在“快速”结账行输入项目数。然后,它向用户请求商品的价格和数量,并打印小计。一旦用户输入的所有项目均已入账,程序将显示所有小计的总数。我已经到了最后一部分,我需要把用户的小计加起来。任何帮助都将不胜感激 def main(): total = 0 while True: item = int(input("How many different items are you buying? ")) if item in
def main():
total = 0
while True:
item = int(input("How many different items are you buying? "))
if item in range (1, 10):
total += subtotal(item)
print("Total of this order $", format (total, ',.2f'), sep='')
break
else:
print("***Invalid number of items, please use a regular checkout line***")
break
def subtotal(item):
total = 0
for item in range(item):
unit_price = float(input("Enter the unit price of the item "))
quantity = int(input("Enter the item quantity "))
subtotal = unit_price * quantity
print("Subtotal for this item: $", format (subtotal, ',.2f'), sep='')
return subtotal
main()
您的代码有大量错误
def subtotal(item):
total = 0
for item in range(item):
unit_price = float(input("Enter the unit price of the item "))
quantity = int(input("Enter the item quantity "))
subtotal = unit_price * quantity
print("Subtotal for this item: $", format (subtotal, ',.2f'), sep='')
total += subtotal
return total
好的,谢谢你的反馈。每个作业的小计数学部分必须在“小计”函数中。我玩列表已经有一段时间了,似乎无法将它从列表转换为int。请澄清最后一点。@Mattr42哦!您可以在“列表中的i”函数中使用“list.append(subtotal(number))!还是我误会了?