017--python基础作业

一、练习题:
1、使用while循环输入 1 2 3 ... 8 9 10
2、求1-100的所有数的和 3、输出 1-100 内的所有奇数 4、输出 1-100 内的所有偶数 5、求1-2+3-4 ... 99的所有数的和
# 练习题:
# 1、使用while循环输入 1 2 3 ... 8 9 10
count = 0
while count < 10:
    number = input('>>:').strip()
    print(number)
    count+=1

# 2、求1-100的所有数的和
print(sum(range(1,101)))

# 3、输出 1-100 内的所有奇数
for i in range(1,100):
    if i%2!=0:
        print(i)

# 4、输出 1-100 内的所有偶数
for i in range(1,100):
    if i%2==0:
        print(i)

# 5、求1-2+3-4 ... 99的所有数的和
s=0
for n in range(1,100):
    if n%2==0:
        n=-n
    s+=n
print (s)
练习题 Code
二、模拟登陆
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户"
# 模拟登陆
#1. 用户输入帐号密码进行登陆
#2. 用户信息保存在文件内
#3. 用户密码输入错误三次后锁定用户"
import sys
count = 0
flag_break =False
while count<3:
    user_name = input('用户名:').strip()
    with open('lock_file',encoding='utf-8') as f_lock:
        for line in f_lock:
            if line.strip() == user_name:
                sys.exit('%s 已经被锁定' % user_name)
    password =input('密码:').strip()
    with open('user_file',encoding='utf-8') as f_user:
        for line in f_user:
            username,passwd = line.strip().split()
            if user_name == username and password == passwd:
                print('登录成功')
                flag_break = True
                break
        if flag_break == False:
            if count == 2:
                count += 1
            else:
                print('用户名或者密码错误,请重试,还有 %s次机会' % (2 - count))
                count += 1
        else:
            break
else:
    print('多次登录错误,此账户已经被锁定')
    with open('lock_file', 'a+') as f_lock:
        f_lock.write('\n' +user_name)
模拟登陆 Code

 三、三级菜单:

1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 返回上一级菜单和顶部菜单
4. 菜单数据保存在文件中"
{
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}
菜单文件 Code
current_layer = {}
last_layers = []
with open('menu',encoding='utf-8') as menu_file:
    f = menu_file.read()
    file_str = str(f)             # 将文本信息转成字符串格式
current_layer = eval(file_str)    # 字符串转成字典格式

while True:
    for key in current_layer:
        print(key)
    choice = input('>>:').strip()
    if len(choice) == 0:continue

    if choice in current_layer:  #进入下一层
        last_layers.append(current_layer)      #将上一层加入列表,待后期返回调用
        current_layer = current_layer[choice]  #将下一层更新为当前层

    if choice == 'b':          #返回上一层
        if last_layers:        #保证列表不为空
            current_layer = last_layers[-1]   #取上一层更新为当前层
            last_layers.pop()                 #删除列表的最后一个

    if choice == 'q':break    #退出

    if choice == 'top':       #返回顶层
        current_layer = last_layers[0]
三级菜单 Code
四、购物车
1. 商品信息- 数量、单价、名称
2. 用户信息- 帐号、密码、余额
3. 用户可充值
4. 购物历史信息
5. 允许用户多次购买,每次可购买多件
6. 余额不足时进行提醒
7. 用户退出时 ,输出档次购物信息
8. 用户下次登陆时可查看购物历史
9. 商品列表分级
# 购物车
#1. 商品信息- 数量、单价、名称
#2. 用户信息- 帐号、密码、余额
#3. 用户可充值
#4. 购物历史信息
#5. 允许用户多次购买,每次可购买多件
#6. 余额不足时进行提醒
#7. 用户退出时 ,输出当次购物信息
import sys
flag_break =False
old_user = True
while True:
    user_name = input('用户名:').strip()
    with open('old_user',encoding='utf-8') as f_old:
        for line in f_old:
            if line.strip() == user_name:
                old_user = True   #判断是老用户标志位
    password =input('密码:').strip()
    with open('shopping_user',encoding='utf-8') as f_user:
        for line in f_user:
            username,passwd,balance = line.strip().split()
            if user_name == username and password == passwd:
                print('登录成功,您的余额:%s'%balance)
                salary = int(balance)
                flag_break = True
        if not flag_break:
            sys.exit('登录失败')
    if old_user:     #执行对于老用户打印历史购物信息的工作
        with open('shopping_history',encoding='utf-8') as f_history:
            file_history = f_history.read()
            print('您以前购买过以下商品'.center(50,'-'))
            print('id       商品      数量    单价    总价')
            print(file_history.strip())
            print('end'.center(60,'-'))

    shopping_cart = {} #购物车
    product_list = [
                    ['自行车',999],
                    ['充电宝',555],
                    ['茅台酒',650],
                    ['铂金笔',500],
                    ['电饭锅',200]
    ]
    while True:
        index = 0
        for product in product_list:
            print(index,product)
            index+=1
        choice = input('请输入商品编号,或者q直接退出')
        if choice=='q':
            print('您已购买以下商品'.center(50,'-'))
            id_count = 1
            total_cost = 0
            print('id       商品      数量    单价    总价')
            for key in shopping_cart:
                msg = ("%s\t\t%s\t\t%s\t\t%s\t\t%s"
                      %(id_count,
                        key,
                        shopping_cart[key][1],
                        shopping_cart[key][0],
                        shopping_cart[key][1]*shopping_cart[key][0]
                        )
                      )
                print(msg)
                id_count+=1
                total_cost+=shopping_cart[key][1]*shopping_cart[key][0]
                with open('shopping_history', 'a+', encoding='utf-8') as f_history:
                        f_history.write('\n' + msg)
            print('您的总花费为:',total_cost)
            print('您的余额为:'+str(salary))
            print('end'.center(60,'-'))
            with open('old_user', 'a+',encoding='utf-8') as f_old:
                f_old.write('\n' + user_name)
            sys.exit()

        number = input('请输入购买数量:')
        if choice.isdigit() and number.isdigit():
            choice = int(choice)
            number = int(number)
            if choice >= 0 and len(product_list) >= choice:
                product = product_list[choice]
                if product[1]*number <= salary:
                    if product[0] in shopping_cart:
                        shopping_cart[product[0]][1] += number #[price ,数量] 只需要把数量+1 ,加入购物车
                    else:
                        shopping_cart[product[0]] = [product[1],number]
                    salary-=product[1]*number
                    print('您已购买 %s个%s ,您的余额为:%s'%(number,product[0],salary))
                else:
                    print("商品的价格为:%s,购买数量%s个,还差%s元"%(product[1],number,product[1]*number-salary))
                    more_money = input('你想要充值吗?y/n:').strip()
                    if more_money == 'n':
                        sys.exit()
                    if more_money == 'y':
                        recharge = int(input('充值:').strip())
                        salary+=recharge
            else:
                print('商品编号不存在,请重新输入')

        else:
            print('编号不存在,请重新输入')
购物车 Code

 

posted @ 2017-04-28 17:21  _慕  阅读(307)  评论(0编辑  收藏  举报
Title
返回顶部