欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python小练习

程序员文章站 2022-12-22 22:53:26
2018.12.1 周末练习: 1.用户三次登陆 2.购物车: ......

2018.12.1

周末练习:

1.用户三次登陆

from random import randint
i = 1
while i < 4:
    num = 0
    verify_code = ''
    while num < 4:
        verify_code = verify_code + chr(randint(65, 90)) #将随机生成的4个字符连接起来
        num += 1
    print(verify_code)
    username = input('请输入用户名:').strip()
    password = input('请输入密码:').strip()
    v_code = input('请输入验证码').upper()
    if username == 'alex' and password == '123':
        if v_code == verify_code:
            print('登陆成功')
            break
        else:
            print('验证码输入错误,请重新输入')
    else:
        # print('账号或密码输入有误!,还有%s次机会请重新输入' % (3-i))
        if username == 'alex' and password != '123':
            print('密码错误,还有%s次机会请重新输入' % (3-i))
        else:
            print('没有此账号,还有%s次机会请重新输入' % (3-i))
    i += 1

2.购物车:

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

while 1:
    username = input('请输入用户名:').strip()
    password = input('请输入密码:').strip()
    shopping = {}if username == 'alex' and password == '123':
        money = int(input('请输入工资'))
        yu_e = 0
        i = 0
        while 1:
            while i < len(goods):
                print('''
                商品编号    商品名    价格
                   %s        %s        %s''' % ((i + 1), goods[i]['name'], goods[i]['price']))
                i += 1
            buy_bianhao = input('请输入要购买的商品编号(按q退出):').strip()
            if buy_bianhao.upper() == 'q':
                if len(shopping) > 0:
                    print('购买的商品:')
                    for k, v in shopping.items():#遍历shopping中的key和value
                        print('商品:%s, 价格:%s' % (k, v))
                    print('余额为:%s' % money)
                    break
                else:
                    print('没有买东西')
                    break
            else:
                index = int(buy_bianhao) - 1
                if money - goods[index]['price'] >= 0:
                    print('成功购买')
                    money -= goods[index]['price']   #剩余工资
                    shopping[goods[index]['name']] = goods[index]['price']  #将购买的商品作为key,price作为value,存入字典shopping中
                    # print('剩余%s:' % money)
                    # print(shopping)#
                else:
                    print("您的余额不足.")
        if buy_bianhao.upper() == 'q':  #判断是否退出
            break

    else:
        print('账号或密码不正确,请重新输入')

 

上一篇: 定时器

下一篇: 前端(八)之形变