第一章作业99乘法表的四种输出/统计制定目录大小/模拟取款机功能

# 1) 使用 while 和 for…in 两个循环分别输出四种九九乘法表效果(共计 8 个)
#第一种 while 循环
i = 1
while i <=9:
    j = 1
    while j<=i:
        print('{}*{}={:<4}'.format(j,i,j*i),end='')
        j += 1
    i += 1
    # 只是为了换行【到达终止值,换一行】
    print()#自带end = /n
print('-'*80)

# 方式二
i = 9 #乘数
while i>=1:
    j = 1 #被乘数
    while j <= i:
        print('{}*{}={:<4}'.format(j,i,j*i),end='')
        j += 1
    i -= 1
    print()
print('-'*80)

# 方式三
# 使用空格输出占位

# define i
i = 1
while i <= 9:
    #define j
    j = 8
    while i <= j:
        print('{:<8}'.format(' '),end='')
        j -= 1
    # define k
    k = i
    while k >= 1:
    #输出乘法表
        print('{}*{}={:<4}'.format(k,i,k*i),end='')
        k -= 1
    i += 1
    print()
print('-'*80)

# 第四种
i = 9
while i >=0:
    j = 9
    while j > 0:
        if j <= i:
            print('{}*{}={:<4}'.format(j,i,j*i),end='')
        #满足j<=i不执行,不满足执行空格填充
        else:
            print('{:<8}'.format(' '),end='')
        j -= 1
    i -= 1
    print()
print('-'*80)`在这里插入代码片`



# 2) 使用文件和目录操作,定义一个统计指定目录大小的函数(注意目录中还有子目录)

import os
size = 0

def get_dir_size(target_dir):
    global size
    d_list = os.listdir(target_dir) #listdir()-- 获取指定文件夹中的
    print(d_list)

    for f in d_list:
        #把每个文件绝对路径进行拼接
        f = os.path.join(target_dir, f)
        #判断变量f是不是文件
        if os.path.isfile(f):
            #是文件进行累加
            size += os.path.getsize(f)
            #是文件夹就重新调用函数继续深入文件进行计算
        if os.path.isdir(f):
            get_dir_size(f)
    return size

#调用函数
dir_size = get_dir_size('/Users/michael/Desktop/第一周作业')

#获取文件的大小
print('total size:',dir_size)



# 参考第 9 节的综合案例中的学生信息管理,来实现一个自动取款机的存取款模拟效果。要求有登陆和退出、查询余额、取钱,存钱等操作。

'''
陈老板钱庄所需功能
1、用户数据
2、交互界面
3、登陆
4、余额查询
5、取款
6、存款
7、退出
'''

#1\ 用户数据
account_data = [
    {'account':'123','password':'123456','deposit':2000},
    {'account':'456','password':'356789','deposit':1000},
    {'account':'789','password':'578394','deposit':1223}]

#定义一个空列表为了记住当前操作用户信息
account_list = []
#2\交互界面

def show_option():
    print('*' * 20, '欢迎来到陈老板钱庄', '*' * 20)
    print('{:15} {:{}<13} {:<15}'.format(' ', '1. 登陆', chr(12288), '2. 余额'))
    print('{:15} {:{}<13} {:<15}'.format(' ', '3. 取款', chr(12288), '4. 存款'))
    print('{:26}{:30}'.format(' ','5. 登出'))
    print('=' * 57)

# 测试交互界面
# show_option()

#3/登陆
def login():
    '''
    判断用户的登陆信息是否存在于account_data中
    :return:
    '''
    #声明accountlist是全局变量,方便登陆函数添加当前登陆用户的信息给其他函数用
    global account_list
    #让用户输入自己的账户密码
    account = input('请输入您的账户:')
    password = input('请输入您的密码:')

    for i, account_dict in enumerate(account_data):
            # 判断用户信息是否正确
        if account_dict['account'] == account and account_dict['password'] == password:
            #当用户信息存在的时候,把索引和用户信息存放到accountlist列表中
            # account_list.append(i)
            account_list.append(account_dict)

    # 判断列表是不是为空
    if account_list != []:
            # 不为空是,代表用户输入的用户存在,则提示登陆成功
        print('{:^50}'.format('------登陆成功------\n'))
        # 否则就是没有找到用户的数据,代表密码或账户错误
    else:
            # 提示用户需不需要重新输入,输入y重新引用本函数,否则结束本函数
        if input('您输入的密码有误,是否重新输入?y/n') == 'y':
           login()
# # 测试登陆


# 4/余额查询

def deposit_money():
    '''
    查询用户账户余额
    :return:
    '''
    print('*' * 20, '欢迎来到陈老板钱庄', '*' * 20)
    print(' '*20,'您的账户余额为{:^8}元!'.format(account_list[0]['deposit']))
    print('='*60)


# 5/存钱

def save_money():
    '''
    存钱操作
    :return:
    '''
    money = float(input('请输入您需要存入的金额:')) #用is_integer判断一个浮点是否为整数
    #用户输入完金额之后在余额的基础上累加
    if money.is_integer() == False:
        print('please enter integer!!!')
        save_money()
    else:
        #把用户输入的金额加入到用户数据中去
        account_list[0]['deposit'] += money
        print('*'*60)
        print(' '*15,'You are successfully save in {:^0} yuan!'.format(money))
        print('*'*60)

#6/取钱操作

def draw_money():
    '''
    用户取钱
    :return:
    '''
    draw_amount = float(input('请输入你需要取出的金额:')) #一样判断是否为整数
    if draw_amount.is_integer() == False:
        print('请输入整数!!!')
        draw_money()

    if account_list[0]['deposit'] >= draw_amount:
        account_list[0]['deposit'] -= draw_amount
        print('*' * 60)
        print(' ' * 15, 'You are successfully draw out {:^0} yuan!'.format(draw_amount))
        print('*' * 60)
    else:
         print('=' * 78)
         print('{:^78}'.format('余额不足!!!'))
         print('=' * 78)
         interact_sys()

# 7/退出
def logOut():
    '''
    登出,可扩展成退出时保存用户操作的信息到数据库
    :return:
    '''
    print('='*78)
    print('='*30,'感谢使用,再见!!!','='*29)
    print('=' * 78)

# 总控制
def interact_sys():
    '''
    界面控制,各个功能的调用
    :return:
    '''
    #使用'死循环'while循环可以保证程序在用户输入完成以后可以继续使用
    #调用界面
    while True:
        show_option()
        key = input('option number:')
        if key == '1':
            #判断用户有没有登陆
            if account_list != []:
                print('-'*10,'您已经登陆,如要登陆新账户,请先退出!','-'*10)
            else:
                login()
        elif key =='2':
            #首先判断用户是否有登陆
            if account_list == []:
                print('-'*20,'please login first!!!!','-'*20)
            else:
                deposit_money()
                input('enter any key for return')
        elif key == '3':
            if account_list == []:
                print('-' * 20, 'please login first!!!!', '-' * 20)
            else:
                draw_money()
                input('enter any key for return')
        elif key == '4':
            if account_list == []:
                print('-' * 20, 'please login first!!!!', '-' * 20)
            else:
                save_money()
                input('enter any key for return')
        elif key == '5':
            #登出函数
            logOut()
            break
        else:
            print('wrong enter,please try again!!')



interact_sys()

猜你喜欢

转载自blog.csdn.net/weixin_42873348/article/details/107045902