【问题标题】:How to Return a Shopping Cart Total如何返回购物车总计
【发布时间】:2015-10-11 09:22:14
【问题描述】:

我已经做了好几个小时了,但我似乎无法弄清楚如何返回总数。我所知道的是我需要从 total = 0 开始。我已经尝试过“for i in x”循环,我希望我能让它们在这里发布,但我一直在反复试验很长时间,以至于我不能准确地回忆起我所做的事情。我正在使用 Python 2.7.10,我是一个完整的初学者。请帮忙?即使只是一个提示?

f_list = []
print 'Enter a fruit name (or done):',
f = raw_input()
while f != 'done':
    print 'Enter a fruit name (or done):',
    f_list.append(f)
    f = raw_input()
print ""
p_list = []
for i in f_list:
    print 'Enter the price for ' + i + ':',
    p = float(raw_input())
    p_list.append(p)
print ""
print 'Your fruit list is: ' + str(f_list)
print 'Your price list is: ' + str(p_list)
print ""
n = len(f_list)
r = range(0,n)
q_list = []
for i in r:
    print str(f_list[i]) + '(' + '$' + str(p_list[i]) + ')',
    print 'Quantity:',
    q = raw_input()
total = 0

【问题讨论】:

  • 你不明白哪一部分?您需要收集水果的数量并为每个水果求和quantity * price

标签: python cart shopping


【解决方案1】:

您忘记创建数量列表,这无济于事。 然后只需遍历您的 f_list 并将它们相加。

f_list = []
print 'Enter a fruit name (or done):',
f = raw_input()
while f != 'done':
    print 'Enter a fruit name (or done):',
    f_list.append(f)
    f = raw_input()
print ""
p_list = []
for i in f_list:
    print 'Enter the price for ' + i + ':',
    p = float(raw_input())
    p_list.append(p)
print ""
print 'Your fruit list is: ' + str(f_list)
print 'Your price list is: ' + str(p_list)
print ""
q_list = []
for i in range(len(f_list)):
    print str(f_list[i]) + '(' + '$' + str(p_list[i]) + ')',
    print 'Quantity:',
    q = raw_input()
    q_list.append(q)
total = 0
for i in range(len(f_list)):
    total += float(p_list[i]) * int(q_list[i])
print "Basket value : ${:.2f}".format(total) 

【讨论】:

    【解决方案2】:

    这个网站上肯定有其他答案可以帮助你,但你想将你的 q 转换为浮点数并将 q 附加到 q_list 就像 p 一样。要获得总计,您只需total = sum(q_list) 并显示您的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多