《python基礎教程》答案(第三章)

《python基礎教程》答案(第三章)

3.2

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

3.3

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

3.4

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

print("The milk will expire on : %i / %i / %i" % milk_expiration)

3.5

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

print("The milk will expire on : %i / %i / %i" % milk_expiration)

milk_carton = { "expiration_date": milk_expiration, "size": 5, "cost": 22.5, "brand": "Thise"}

3.6

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

print("The milk will expire on : %i / %i / %i" % milk_expiration)

milk_carton = { "expiration_date": milk_expiration, "size": 5, "cost": 22.5, "brand": "Thise"}

print(" This is %i oz of %s milk. It will expire on : %i / %i / %i" % ( milk_carton["size"], \
                                                                        milk_carton["brand"], \
                                                                        milk_carton["expiration_date"][0], \
                                                                        milk_carton["expiration_date"][1], \
                                                                        milk_carton["expiration_date"][2]
                                                                        ))

3.7

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

print("The milk will expire on : %i / %i / %i" % milk_expiration)

milk_carton = { "expiration_date": milk_expiration, "size": 5, "cost": 22.5, "brand": "Thise"}

print(" This is %i oz of %s milk. It will expire on : %i / %i / %i" % ( milk_carton["size"], \
                                                                        milk_carton["brand"], \
                                                                        milk_carton["expiration_date"][0], \
                                                                        milk_carton["expiration_date"][1], \
                                                                        milk_carton["expiration_date"][2]
                                                                        ))

print(" 6 cartons of milk will set you back : %i gold coins" % (milk_carton["cost"] * 6))

3.8

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

print("The milk will expire on : %i / %i / %i" % milk_expiration)

milk_carton = { "expiration_date": milk_expiration, "size": 5, "cost": 22.5, "brand": "Thise"}

print(" This is %i oz of %s milk. It will expire on : %i / %i / %i" % ( milk_carton["size"], \
                                                                        milk_carton["brand"], \
                                                                        milk_carton["expiration_date"][0], \
                                                                        milk_carton["expiration_date"][1], \
                                                                        milk_carton["expiration_date"][2]
                                                                        ))

print(" 6 cartons of milk will set you back : %i gold coins" % (milk_carton["cost"] * 6))

cheese_list = ["cheese1", "cheese2"]
dairy_section.extend(cheese_list)
print(dairy_section)

index = 0
cheese_indexes = []
for product in dairy_section:
    if "cheese" in product:
        print("cheese found at index %i : %s" % (index, product))
        cheese_indexes.append(index)
    index += 1

cheese_indexes.sort(reverse=True)
print(cheese_indexes)

new_cheese_list = []
for index in cheese_indexes:
    new_cheese_list.append(dairy_section.pop(index))

print(dairy_section)

3.9

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

print("The milk will expire on : %i / %i / %i" % milk_expiration)

milk_carton = { "expiration_date": milk_expiration, "size": 5, "cost": 22.5, "brand": "Thise"}

print(" This is %i oz of %s milk. It will expire on : %i / %i / %i" % ( milk_carton["size"], \
                                                                        milk_carton["brand"], \
                                                                        milk_carton["expiration_date"][0], \
                                                                        milk_carton["expiration_date"][1], \
                                                                        milk_carton["expiration_date"][2]
                                                                        ))

print(" 6 cartons of milk will set you back : %i gold coins" % (milk_carton["cost"] * 6))

cheese_list = ["cheese1", "cheese2"]
dairy_section.extend(cheese_list)
print(dairy_section)

index = 0
cheese_indexes = []
for product in dairy_section:
    if "cheese" in product:
        print("cheese found at index %i : %s" % (index, product))
        cheese_indexes.append(index)
    index += 1

cheese_indexes.sort(reverse=True)
print(cheese_indexes)

new_cheese_list = []
for index in cheese_indexes:
    new_cheese_list.append(dairy_section.pop(index))

print(dairy_section)

print(len(new_cheese_list))

3.10

# created by swy
"""
solutions to beginning python
"""

dairy_section = ["milk", "cream", "cheese", "butter"]

print("%s %s" % (dairy_section[0], dairy_section[-1]))

milk_expiration = (11, 31, 2011)

print("The milk will expire on : %i / %i / %i" % milk_expiration)

milk_carton = { "expiration_date": milk_expiration, "size": 5, "cost": 22.5, "brand": "Thise"}

print(" This is %i oz of %s milk. It will expire on : %i / %i / %i" % ( milk_carton["size"], \
                                                                        milk_carton["brand"], \
                                                                        milk_carton["expiration_date"][0], \
                                                                        milk_carton["expiration_date"][1], \
                                                                        milk_carton["expiration_date"][2]
                                                                        ))

print(" 6 cartons of milk will set you back : %i gold coins" % (milk_carton["cost"] * 6))

cheese_list = ["cheese1", "cheese2"]
dairy_section.extend(cheese_list)
print(dairy_section)

index = 0
cheese_indexes = []
for product in dairy_section:
    if "cheese" in product:
        print("cheese found at index %i : %s" % (index, product))
        cheese_indexes.append(index)
    index += 1

cheese_indexes.sort(reverse=True)
print(cheese_indexes)

new_cheese_list = []
for index in cheese_indexes:
    new_cheese_list.append(dairy_section.pop(index))

print(dairy_section)

print(len(new_cheese_list))

print(new_cheese_list[0][:6])

第三章 完

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章