Python小程序练习及认识小数据池和编码

 
1. 三次登录验证
    完成⽤户登录验证
    要求:
        1. 系统⾃动⽣成4位随机数. 作为登录验证码.
        2. ⽤户输入⽤用户名和密码还有验证码.
        3. 用户可以有三次登录的机会. 但是验证码如果输入错误. 不计算在内
复制代码
from random import randint

count = 1
while count <= 3:
    num = 0
    verify_code = ""
    while num < 4:
        verify_code += chr(randint(65, 90))
        num += 1

    # 用户登录
    username = "alex"
    password = "123"

    # 用户输入
    uname = input("请输入你的用户名:" )
    upwd = input("请输入你的密码:")
    vi_code = input(f"请输入你的验证码{verify_code}:")

    # 判断验证码
    if vi_code.upper() == verify_code.upper():
        print("验证码正确")
        # 判断用户名和密码
        if uname == username and upwd == password:
            print("登录成功")
            break
        else:
            print("登录失败(还剩下%s次机会)" % (3-count))
            count += 1
    else:
        print("验证码错误")
复制代码
2.商品信息:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
用户信息:
user = {"username":"alex", "password": "123456"}


功能要求:
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、退出时,打印已购买商品和余额
复制代码
# 商品信息:
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
# 用户信息:
user = {"username":"alex", "password": "123456"}
shoppingcart = []
while 1:
    uname = input("请输入你的用户名:")
    upwd = input("请输入你的密码:")
    if uname == user['username'] and upwd == user['password']:
        print("登录成功!")
        money = int(input("请输入你兜里钱:"))

        while 1:
            for i in range(len(goods)):
                print(i+1, goods[i]['name'], goods[i]['price'])
                # 1 键盘 100  0
                # 2 鼠标 200  1
                # 3 美女 1000 2
            # 2
            num = int(input("请输入你要购买的商品编号:")) 
            # 还原回索引
            index = num - 1
            # 获取到购买的商品
            good = goods[index]
            # 判断是否可以购买该商品
            if money >= good['price']:
                # 判断是否已经购买过该商品, 如果购买过. 数量+1 没买过 加到购物车里
                for el in shoppingcart:      # el:你已经够买过的商品
                    if el['id'] == index:      # 买过
                        el['totle'] += 1         # 数量+1
                        break     # 继续显示商品列表
                else:   # 没买过
                    shoppingcart.append({"id": index, "name": good['name'], "price": good['price'], "totle": 1})
                money -= good['price']    # 扣钱
                print("购买成功!!, 您的余额是%s" % money)
            else:
                print("对不起. 您的余额已不足!, 您的余额还剩%s" % money) 
            # 是否继续购物
            isContinue = input("请问是否继续购买商品(Y/N)")
            if isContinue.upper() == "N":
                # 打印购买的商品和余额
                for g in shoppingcart:
                    print(g['name'], g['price'], g['totle'])
                print("你还剩下%s" % money)
                # 程序退出
                exit() # 程序退出
    else:
        print("登录失败!")
复制代码
 
1. 小数据池, id()
            小数据池针对的是: int, str, bool
            在py文件中几乎所有的字符串都会缓存.
            id() 查看变量的内存地址
 
 2. is和==的区别
            is 比较的是内存地址
            == 比较的是内容
            当两个变量指向同一个对象的时候. is是True, ==也是True
       
 3. 编码
                1. ascii. 有: 数字, 字母, 特殊字符. 8bit  1byte 128  最前面是0
                2. gbk. 包含: ascii, 中文(主要), 日文, 韩文, 繁体文字. 16bit, 2byte.
                3. unicode. 包含gbk,ascii,big5... 32bit, 4byte
                4. utf-8. 可变长度的unicode.
                    1. 英文: 8bit,1byte
                    2. 欧洲文字: 16bit 2byte
                    3. 中文: 24bit 3byte
            不同的编码之间不能随意转换. 中国人gbk和德国人utf-8骂 想要沟通必须通过英文(unicode)(媒介)
            在python3中. 默认的编码是unicode,我们的字符串就是unicode
            在python2中. 默认的编码是ASCII.  Cpython.c语言的默认编码是ASCII
            unicode弊端:在存储和传输的时候. 是很浪费的
            在存储和传输的时候不能直接使用unicode. 必须要对字符串进行编码. 编码成bytes类型
            bytes: 字节形式的字符串
                1. encode(编码格式) 编码
                2. decode(编码格式) 解码
            bytes是一种另类的字符串表示形式
            "哈哈哈" => xeexabx13xeexabx13xeexabx13
复制代码
bs = b'xe6x88x91xe4xbbx8axe5xa4xa9xe9x9dx9exe5xb8xb8xe7x9ax84xe5x9bxb0'
# 把这个bytes转化成gbk的bytes
s = bs.decode("utf-8")
s = s.encode("gbk")
print(s)
复制代码

    关于bytes, 非ascii中的内容. 展示的时候都是x.. 如果是ascii中的内容. 原样输出

原文地址:https://www.cnblogs.com/selina1997/p/10060654.html