taqueria.py 之total_sum的位置

1

menu  = {
    "Baja Taco": 4.25,
    "Burrito": 7.50,
    "Bowl": 8.50,
    "Nachos": 11.00,
    "Quesadilla": 8.50,
    "Super Burrito": 8.50,
    "Super Quesadilla": 9.50,
    "Taco": 3.00,
    "Tortilla Salad": 8.00
}
total = []
total_sum = 0
#ask prompt from user
while True:
    try:
        item = input("Item: ")
        item = item.title()
        price = menu.get(item)
#calculate the total price
        total.append(price)
        if price is not None:
            for i in total:
                print(f"i:{i}")
                total_sum  += i
                print(f"{total_sum}")
            print(f"Total: ${total_sum:.2f}")
    except EOFError:
        print("")
        break

                                                                                                                                                                                                                                                                                           
python25/pset3/taqueria/ $ python taqueria.py
Item: taco
i:3.0
3.0
Total: $3.00
Item: taco
i:3.0
6.0
i:3.0
9.0
Total: $9.00
2次输入“taco” 之后的total_sum竟然等于$9,明显不对!
原来每次都迭代了整个list,而i不是当前的单个元素。
第1次:total = [3.0]   0 + 3.0 = 3.0(total_sum)
第2次:total = [3.0,3.0]  3.0 +( 3.0 + 3.0) = 9.0 (total_sum)

2

改变total_sum的位置后,输出是正确的
menu  = {
    "Baja Taco": 4.25,
    "Burrito": 7.50,
    "Bowl": 8.50,
    "Nachos": 11.00,
    "Quesadilla": 8.50,
    "Super Burrito": 8.50,
    "Super Quesadilla": 9.50,
    "Taco": 3.00,
    "Tortilla Salad": 8.00
}
total = []
#ask prompt from user
while True:
    try:
        item = input("Item: ")
        item = item.title()
        price = menu.get(item)
#calculate the total price
        total.append(price)
        if price is not None:
            total_sum = 0
            for i in total:
                print(f"i:{i}")
                total_sum  += i
                print(f"{total_sum}")
            print(f"Total: ${total_sum:.2f}")
    except EOFError:
        print("")
        break

                                                                                                                                                                                                                                                                                                 
python25/pset3/taqueria/ $ python 2.py
Item: taco
i:3.0
3.0
Total: $3.00
Item: taco
i:3.0
3.0
i:3.0
6.0
Total: $6.00
可以看到:
每次total_sum都被重置为0,
这样每一次,就只加了新list
第1次:total = [3.0]   0 + 3.0 = 3.0(total_sum)
第2次:total = [3.0,3.0]  0 +( 3.0 + 3.0) = 6.0 (total_sum)

3

实际上,这里不需要for也可以
sum(iterable)
menu  = {
    "Baja Taco": 4.25,
    "Burrito": 7.50,
    "Bowl": 8.50,
    "Nachos": 11.00,
    "Quesadilla": 8.50,
    "Super Burrito": 8.50,
    "Super Quesadilla": 9.50,
    "Taco": 3.00,
    "Tortilla Salad": 8.00
}
total = []
total_sum = 0
#ask prompt from user
while True:
    try:
        item = input("Item: ")
        item = item.title()
        price = menu.get(item)
#calculate the total price
 	    total.append(price)
         if price is not None:
            total_sum = sum(total)
            print(f"Total: ${total_sum:.2f}")
    except EOFError:
        print("")
        break


                                                                                                                                                                                                                                                                                                   

4 TypeError问题

在这里插入图片描述

注意到,可能存在一个问题
 total.append(price)
         if price is not None:
         这2行的顺序
输入None后产生typeerror
保持list干净,还是先if之后,
再添加到list里面
(以上同理)
#calculate the total price
        if price is not None:
            total.append(price)
            total_sum = sum(total)
            print(f"Total: ${total_sum:.2f}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值