• 函数参数补充、名称空间与作用域、global与nonlocal 练习


    # 作业要求:下述所有代码画图以及分析代码执行流程
    # 1、以定义阶段为准,先画出名称空间的嵌套关系图
    # 2、然后找到调用函数的位置,写出函数调用时代码的执行过程,涉及到名字的查找时,参照1中画好
    # 的嵌套图,标明查找顺序,一层一层直到找到位置

    # ===================题目一===================
    input=333
    def func():
    input=444
    func()
    print(input)

    # ===================题目二===================
    def func():
    print(x)
    x=111

    func()

    # ===================题目三===================
    x=1
    def func():
    print(x)


    def foo():
    x=222
    func()

    foo()

    # ===================题目四===================
    input=111
    def f1():
    def f2():
    # input=333
    print(input)
    input=222

    f2()

    f1()

    # ===================题目五===================
    x=111
    def func():
    print(x) #
    x=222

    func()

    # ===================题目六===================
    x=111

    def foo():
    print(x,)

    def bar():
    print(x)

    foo()
    bar()

    # ===================题目七===================
    x=1
    def func2():
    func1()

    x=2
    def func1():
    print(x)

    x=3

    func2()

    # ===================题目八===================
    # 1、如下全局变量记录了当前登录用户,编写登录功能,一旦用户登录成功,则将全局变量赋值为当前登录的用户名
    # login_user=None
    '''
    login_user=None
    def users(file,user_input,pas):
    with open(file, mode='rt', encoding='utf-8') as f1:
    for line in f1:
    user, word = line.strip().split(':')
    if user_input == user and pas == word:
    print('登录成功!')
    global login_user
    login_user=user_input
    else:
    print('登录失败')

    users('dbs.txt','tank','123')
    print(login_user)
    '''
    # 2、针对之前编写的查询余额的功能,添加额外的逻辑:如果用户没有登录,则先执行登录功能
    '''
    login_user=None
    msg = {'0':'退出','1': '充值','2': '转账','3': '提现','4':'查询余额'}
    dic={}
    tag = True
    while tag:
    with open('db.txt', mode='rt', encoding='utf-8') as f2:
    for line2 in f2:
    user2, money = line2.strip().split(':')
    dic[user2] = int(money)
    msg = "'0':'退出','1': '充值','2': '转账','3': '提现','4':'查询余额'"
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
    print('必须输入命令编号的数字')
    continue
    elif cmd not in msg:
    print('输入的命令不存在')
    continue
    else:
    if cmd == '4':
    if login_user not in dic:
    user_input=input('请输入用户名称:')
    pas=input('请输入用户密码:')
    with open('dbs.txt', mode='rt', encoding='utf-8') as f1:
    for line in f1:
    user, word = line.strip().split(':')
    if user_input == user and pas == word:
    user2 = user_input
    money = dic[user2]
    print(money)
    else:
    print('登录失败')
    else:
    user2 = login_user
    money = dic[user2]
    print(money)
    '''
  • 相关阅读:
    第一个只出现一次的字符
    把数组排成最小的数
    整数中1出现的次数
    连续子数组的最大和
    最小的K个数
    数组中出现次数超过一半的数字
    字符串的排列
    二叉搜索树与双向链表
    numpy中ravel()和flatten()
    复杂链表的复制
  • 原文地址:https://www.cnblogs.com/0B0S/p/12524861.html
Copyright © 2020-2023  润新知