博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python购物车进阶(函数)
阅读量:4326 次
发布时间:2019-06-06

本文共 4500 字,大约阅读时间需要 15 分钟。

购物车进阶: 用函数完成登录注册以及购物车的功能。    1,启动程序,用户可选择四个选项:登录,注册,购物,退出。    2,用户注册,用户名不能重复,注册成功之后,用户名密码记录到文件中。    3,用户登录,用户名密码从文件中读取,进行三次验证,验证不成功则退出整个程序。    4,用户登录成功之后才能选择购物功能进行购物,购物功能(就是将购物车封装到购物的函数中)。    5,退出则是退出整个程序。
'''头发掉了不少'''import os'''用户注册'''def register():    username = input("请输入注册用户名:")    password = input("请输入注册密码:")    with open('user.txt', encoding='utf-8', mode='r+') as f1:        for i in f1:            hard = i.split(' ')            if username == hard[0]:                print('用户已存在')                break        else:            f1.write(username + ' ' + password + '\n')            print('{}用户注册成功'.format(username))'''用户登录'''def login():    count = 1    while count <= 3:        username = input('请输入用户名:')        password = input('请输入密码:')        with open('user.txt', encoding='utf-8') as f:            for i in f:                new_line = i.strip().split(' ')                if username == new_line[0] and password == new_line[1]:                    print('登录成功!')                    shopping()                    break            else:                print('用户名或密码错误,请重新输入,还有{}次机会'.format(3 - count))                count += 1                continue            break# login()'''购物车'''def shopping():    good_list = [        {
"name": "iphone", "price": 999}, {
"name": "watch", "price": 99}, {
"name": "耳机", "price": 66}, {
"name": "充电器", "price": 20} ] while True: monny = input('请输入你充值的金额:').strip() if monny.isdigit(): print('成功充值', monny, '元') break else: print('我要钱') # 判断用户输入是不是钱 good_car = [] # 定义购物车商品列表 monny_test = [] # 定义余额 false = True # 用于whlie退出 print("商品列表".center(30, '*')) print('序号'.center(10), '名称'.center(10), '价格'.center(10)) shapp_len = len(good_list) for i in range(shapp_len): print(str(i + 1).center(10), good_list[i]['name'].center(10), str(good_list[i]['price']).center(10)) '''用于输出序列号加商品名及价格''' while false: shopp = input("输入你购买的商品序号q为进入购物车查看:").upper() if shopp.isdigit(): # 判断是是否为数字 if int(shopp) - 1 > shapp_len - 1: # -1为头索引位置 print('输入的序号不在范围类') else: good_car.append((good_list[int(shopp) - 1]['name'], good_list[int(shopp) - 1]['price'])) print('{}已经添加到购物车,价格为{}'.format(good_list[int(shopp) - 1]['name'], good_list[int(shopp) - 1]['price'])) '''把选中商品的内容添加到good_car列表中''' elif shopp == 'Q': print('购物车的商品为'.center(30, '=')) print('商品名'.center(10), '价格'.center(10)) for i in good_car: print(i[0].center(10), str(i[1]).center(10)) monny_test.append(i[1]) Monny = sum(monny_test) # 求和 print('总个数为{}总金额为{}-->充值的金额为{}'.format(len(good_car), Monny, monny)) if int(monny) > Monny: print('购买成功,余额为{}'.format(int(monny) - Monny)) break else: print('你的余额不足,请去掉一下商品根据序号删除:') print('购物车的商品为'.center(30, '=')) print('序号'.center(10), '商品 价格'.center(10)) while True: monny_test2 = [] # 定义删除商品后余额 for i in range(len(good_car)): print(str(i + 1).center(10), good_car[i]) num = int(input('你的余额不足,请去掉一下商品,输入产品序号')) - 1 # -1为头索引位置0 if num < len(good_car): del good_car[num] # 选择删除的商品通过索引位置删除 for i in good_car: print(i[0].center(10), str(i[1]).center(10)) monny_test2.append(i[1]) # 取出删除后的商品价格添加到monny_test2中 Monny = sum(monny_test2) # 求和 print('总个数为{}总金额为{}-你充值的金额为{}'.format(len(good_car), Monny, monny)) if int(monny) >= Monny: # 如果总钱大于购买商品的钱则自动结算 print('购买成功,余额为{}'.format(int(monny) - Monny)) break else: continue else: print('输入超出范围') false = False'''启动程序'''while True: print('欢迎来到taobao——tow世界\n1\t用户登录\n2\t用户注册\n3\t退出购物车') call = input('输入你的选择:') if call == '1': login() break elif call == '2': register() continue elif call == '3': break else: print('输入有误')

充满活力的一天!!!

转载于:https://www.cnblogs.com/jin-yuana/p/9889070.html

你可能感兴趣的文章
PHP Curl发送数据
查看>>
HTTP协议
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>