zoukankan      html  css  js  c++  java
  • 韦大仙python--购物车

    程序:购物车程序

    需求:

    启动程序后,让用户输入工资,然后打印商品列表
    允许用户根据商品编号购买商品
    用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    可随时退出,退出时,打印已购买商品和余额

    !/usr/bin/env python

    -- coding:utf-8 --

    username=lhl,password=123456

    auth:lhl

    print("-------------欢迎登入xx购物广场------------")
    usename=input("请输入您的用户名:")
    pwd=input("请输入您的密码:")
    if usename'lhl' and pwd'123456':
    with open('info.txt','r',encoding='utf-8') as f,
    open('price.txt','r',encoding='utf-8') as p:
    p=p.read()
    f=f.read()
    print('#################购物清单###############')
    print('%s'%f ,'剩余金额:%s'%p)
    product_list=[
    ('iphone',5800),
    ('mac pro',9800),
    ('bike',800),
    ('watch',10600),
    ('coffee',31),
    ('python-book',85)
    ]
    shoping_list=[]
    salary=input("input your salary:")
    if salary.isdigit():
    salary=int(salary)
    while True:
    for index,item in enumerate(product_list):
    print(index,item)
    user_choice=input("选择商品>>:")
    if user_choice.isdigit():
    user_choice=int(user_choice)
    if user_choice <len(product_list) and user_choice >=0:
    p_item=product_list[user_choice]
    if p_item[1] <=salary:
    shoping_list.append(p_item)
    salary-=p_item[1]
    print('添加 %s 到您的购物车,你的余额是:33[0;31m%s33[0m '%(p_item,salary))
    else:
    print('你的余额还剩 33[0;31m%s33[0m 元,无法购买'%salary,'还差:33[0;31m%s33[0m元'%(p_item[1]-salary))
    else:
    print("product code is not exits"%user_choice)
    elif user_choice=='q':
    print("---------shoping list--------------")
    for p in shoping_list:
    print(p)
    with open("info.txt",'a',encoding='utf-8') as f:
    f.write('商品:'+p[0]+'价格:')
    f.write(str(p[1])+' ')
    print("您的余额是:33[0;31m%s33[0m "%salary)
    with open('price.txt','w',encoding='utf-8') as f:
    f.write(str(salary))
    print("-------------欢迎再次光临-------------")
    exit()
    else:
    print("invalid option")

  • 相关阅读:
    sharepoint2010无法连接到配置数据库。
    多选框加和单选框一样的控制,只能选一个
    Windows Server 2008 网站访问PHP响应慢的解决方法
    Windows下的PHP安装文件线程安全和非线程安全的区别
    Apache+PHP 环境上传文件配置
    出现 HTTP Error 503. The service is unavailable 错误
    IIS7 上传 下载文件大小限制的设置
    php 错误信息配置
    新篇章
    面向对象
  • 原文地址:https://www.cnblogs.com/king8/p/9182616.html
Copyright © 2011-2022 走看看