day02_final

1、功能要求:

用户输入河北,则打印河北省下的市,用户输入市,则显示该河北省的这个市下的县

## 功能:
北京: 昌平、海淀
河北: 邯郸、石家庄
山西: 太原、大同

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
dic = {
    "河北": {
        "石家庄": ["鹿泉", "藁城", "元氏"],
        "邯郸": ["永年", "涉县", "磁县"],
    },
    "山西": {
        "太原":["清徐县","平遥县","交城县"],
        "大同":["大同县","天镇县","灵丘县"]
    },
    "北京": {
        "昌平":["沙河","回龙观","龙泽"],
        "海淀":["中关村","四季青","知春路"]
    },
}

while True:
    city=input("请输入想要查看的city:")
    print(city)
    if city not in dic:
        print("输入错误,请重新输入:")
        continue
    print(dic[city])
    xiancheng=input("请输入想要查看的xiancheng:")
    if xiancheng not in dic[city]:
        print("输入错误,请重新输入")
        continue
    print(dic[city][xiancheng])

 2、功能要求:

购物车

要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]

#!/usr/bin/env python3
# -*- conding:utf-8 -*-

shoping=[]

goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]

while True:
    salary=input('Plz input your salary : ')
    if salary.isdigit():
        int(salary)
        break
    else:
        continue
while True:
    print(goods)
    for index , i in enumerate(goods):           #enumerate函数遍历索引及元素。index作为商品编号,i作为商品元素.
        print(index , ',' , i["name"],i["price"] )
    choice = input('请选择要购买的商品编号:')
    salary=int(salary)
    if choice.isdigit():
        choice=int(choice)
        if choice >=0 and choice < len(goods):
            p=goods[choice]
            # print(p)
            if salary >=p['price']:
                salary -= p["price"]
                shoping.append(p)
                print("Added 33[32;1m[%s]33[0m into you shopping cars,and you current balance is 33[31;1m%s33[0m " % (
                    p['name'], salary))
            else:
                print("你的钱不够")
                chongzhi=input('是否需要充值 Y/N:')
                if chongzhi == "Y" or chongzhi == 'y' :
                    salary_chongzhi=input("请输入充值的金额:")
                    if salary_chongzhi.isdigit():
                        salary_chongzhi=int(salary_chongzhi)
                    salary=salary_chongzhi+salary
                else:
                    continue
    elif choice == 'delete':
        for index, i in enumerate(shoping):
            print(index, '.', i['name'], i['price'])
        delete = input('请输入你想要删除的商品编号:')
        if delete.isdigit():
            delete=int(delete)
            # if delete >=0 and delete < len(shoping):
            salary+=shoping[delete]['price']
            shoping.pop(delete)
    elif choice == 'shop':
        print(shoping)

    elif choice=="quit":
        print("你购买的商品列表:",shoping)
        exit()
 
原文地址:https://www.cnblogs.com/zhangray/p/7530381.html