设为首页 收藏本站
查看: 1547|回复: 0

[经验分享] Python学习day2作业总结

[复制链接]
发表于 2018-8-16 12:02:20 | 显示全部楼层 |阅读模式
#!/usr/bin/env python  
# _*_coding:utf-8_*_
  
'''
  
* Created on 2016/10/18 22:01.
  
* @author: Chinge_Yang.
  
'''
  
import os
  
import getpass
  
import datetime
  
menu = {
  
    "Mobile phone": [
  
        ("Iphone7", 6188),
  
        ("Iphone7 plus", 7888),
  
        ("Xiaomi5", 2888)
  
    ],
  
    "Car": [
  
        ("Audi Q5", 490000),
  
        ("Ferrari 488", 4888888)
  
    ],
  
    "Drink": [
  
        ("Milk", 59),
  
        ("Coffee", 30),
  
        ("Tea", 311)
  
    ]
  
}
  
user_file = "user.txt"
  
shopping_log = "shopping.log"
  
money = 0   #初始金额
  
all_cost = 0
  
shopping_cart = {}
  
exit_user_flag = False
  
exit_flag = False
  
exsit_flag = False
  
if not os.path.exists(user_file):
  
    f = open(user_file,'w')
  
    f.close()
  
if not os.path.exists(shopping_log):
  
    f = open(shopping_log,'w')
  
    f.close()
  
def show_shopping_cart ():    #显示购物车,更新用户信息,exit前使用
  
    #显示购物车信息
  
    print("You purchased products as below".center(50,"*"))
  
    print("%-20s %-15s %-10s %-20s" %("Goods","Price","Number","Cost"))
  
    for key in shopping_cart:
  
        p_name = key[0]
  
        p_price = int(key[1])
  
        p_number = int(shopping_cart[key])
  
        print("%-20s %-15s %-10s \033[32;1m%-20s\033[0m" %(p_name,p_price,p_number,p_price*p_number))
  
    print("End".center(50,"*"))
  
    print("%-20s %-15s %-10s \033[32;1m%-20s\033[0m" %("You total cost:","","",all_cost))
  
    print("Your balance is [\033[32;1m%s\033[0m]" % money)
  
    if new_user is True:
  
        # 将新用户信息写入到用户文件中
  
        file = open(user_file, "a")
  
        file.write("%s %s %s\n" % (user_name, user_passwd, money))  # 用户、密码、金钱存入用户文件
  
        file.close()
  
    else:
  
        if old_money != money:  #充值或买了东西
  
            # 将旧用户信息更新到用户文件中
  
            old_user_info = "%s %s %s" % (user_name, user_passwd, old_money)
  
            new_user_info = "%s %s %s" % (user_name, user_passwd, money)
  
            with open(user_file) as file:
  
                info = file.read()
  
            new_info = info.replace(old_user_info,new_user_info)
  
            with open(user_file,"w") as file:
  
                file.write(new_info)
  
while exit_user_flag is not True:
  
    #输入用户密码
  
    user_name = input("Please input your name:").strip()
  
    #user_passwd = input("Please input your password:").strip()
  
    user_passwd = getpass.getpass("\033[1;33mPlease input your password:\033[0m")
  
    if user_name == '' or user_passwd == '':
  
        continue
  
    elif ' ' in user_name:
  
        print("\033[31;1mNot allowed to contain spaces!\033[0m")
  
        continue
  
    #查看是否存在于用户数据库
  
    user_check = open(user_file)
  
    for l in user_check:
  
        l = l.split()
  
        user = l[0]
  
        passwd = l[1]
  
        if user_name == user:   #老用户
  
            new_user = False    #标记为老用户
  
            money = int(l[2])   #记录用户余额
  
            old_money = money   #旧余额
  
            if not passwd == user_passwd:
  
                print ("\033[1;31mYour password is error!\033[0m".center(50,"*"))
  
            else:
  
                print ("\033[1;31mWelcome to go shopping!\033[0m".center(50,"-"))
  
                exit_user_flag = True
  
            break
  
    else:
  
        new_user = True #标记为新用户
  
        exit_user_flag = True
  
    user_check.close()  #关闭
  
if not new_user:    #旧用户
  
    #读取购物历史,判断是否有此用户记录
  
    file = open(shopping_log)
  
    for line in file:
  
        line = line.split()
  
        line_user = line[0]
  
        if line_user == user_name:  #存在记录
  
            exsit_flag = True
  
            break   #跳出检测
  
    file.close()
  
    if exsit_flag is True:   #内容不为空
  
        #只有输入y或者yes才读取显示购物历史,否则不显示
  
        print("Input \033[1;33m[y|yes]\033[0m to view your purchase history,\033[1;33m[others]\033[0m means not.")
  
        see_history = input("Please input:").strip()
  
        if see_history == "y" or see_history == "yes":
  
            # 显示用户购物历史
  
            # output = os.system("grep %s %s" %(user_name,shopping_log))
  
            # print(output.read())
  
            print("User %s shopping history:" % user_name)
  
            print("%-20s %-15s %10s %20s" %("Username","datetime","Number","Goods"))
  
            file = open(shopping_log)
  
            for line in file:
  
                line = line.split("\t")
  
                line_user = line[0]
  
                if line_user == user_name:  #存在记录
  
                    print("%-10s %-15s %10s %20s" % (line[0],line[1],line[2],line[3].strip()))
  
            file.close()
  
        else:
  
            print("You are not to view your purchase history!")
  
            print("-".center(50,"-"))
  
one_layer_list = [] #一级菜单
  
while True:
  
    while True:
  
        #打印各类菜单
  
        print("Species list".center(50,"-"))
  
        for index, item in enumerate(menu):
  
            print("\033[32;1m%d\033[0m --> %s" % (index, item))
  
            one_layer_list.append(item)
  
        print("End".center(50,"-"))
  
        print("[q|b] to quit")
  
        once_choice = input("Input your choice:").strip()
  
        if once_choice.isdigit():   #输入数字
  
            once_choice = int(once_choice)
  
            if 0

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-552645-1-1.html 上篇帖子: Python知识点备忘录 下篇帖子: pip install MySQL-python
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表