Job 3/19

Operational requirements: The following drawing all the code and analysis code execution flow
1, subject to the definition phase, the first nesting relationship shown in FIG namespace
2, and then locate the calling function, execution of function calls to write the code, when it comes to find the name, referring to a good 1-in-picture
nested chart, indicating the search order, layer by layer until you find the location

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

A: func () -> def func (): Block Code -> local variable input = 444 -> print (input) the global variable input = 333 (printing 333)

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

func()

A: func () -> def func (): Block Code -> Global variables: x = 111 -> print (x)

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


def foo():
    x=222
    func()

foo()

A: foo () -> local variable x = 222, func () -> Global variables x = 1, print (x)

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

f1()

A: f1 () -> f2 () -> e layer: input = 222 -> print (input)

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

func()

A: error, x = 222 is marked as local variables, have not been assigned before use

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

def foo():
    print(x,)

def bar():
    print(x)

foo()
bar()

A: foo () -> Global variables x = 111 -> print (x) -> bar () -> x = 111 -> print (x)

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

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

x=3

func2()

答:x =1 ——> x = 2 ——> x = 3 ——> func2() ——> func1() ——> print(x)

=================== title eight ===================
1, the following global variables records the current logged-on user , writing a login function, once the user logs in successfully, then the global variable assignment for the currently logged on user name
2, for the balance of the query written before the function, add additional logic: if the user is not logged in, log in to perform the function

def login():
    global login_user
    while not login_user:
        with open('users.txt','r',encoding = 'utf-8') as f:
            user = input('请输入入账号:').strip()
            pwd = input('请输入密码:').strip()
            for line in f:
                user1,pwd1 = line.strip().split(':')
                if user == user1:
                    if pwd == pwd1:
                        login_user = user
                        print('登陆成功')
                        return
                    else:
                        print('登陆失败')
                        break
            else:
                print('没有这个用户哦')


def look_at_it(user):
    while not login_user:
        login()
    with open('db.txt','r',encoding = 'utf-8') as f1:
        for line in f1:
            user1, money = line.strip().split(':')
            if user == user1:
                print('余额为:{}'.format(money))
                return
        else:
            print('没有这个用户哦')


login_user=None
login()
print(login_user)
look_at_it(login_user)

Guess you like

Origin www.cnblogs.com/pythonwl/p/12526457.html