Python learning day09

Python day 09

A three way function definition

1. Empty function

When you know you need to achieve a certain function, but do not know how to use time code to achieve, you can temporarily empty write a function and then to implement other functions.

def get_test1():
    pass

2. Reference function

There are parameters in the function definition stage brackets, known to have a function parameter. Note that: when there is a reference definition, must also pass parameters means that calling.

If the function code logic relies on the external body of the incoming values, there have to be defined as a function parameter.

def get_max(num1,mun2):
    if num1 > num2:
        print(num1)
    else:
        print(num2)

3. No reference function

No phase function parameter in the brackets, the function is called with no arguments. Note that: when no parameters defined, meaning when there is no need to call the incoming parameters.

If the function does not need to rely on an external body of the incoming code logic value, you have to be defined as a function of no arguments.

def get_test3():
    print('hellow')
get_test3()

Second, the return value of the function

  1. What is the return value

    After some internal code function column logic processing result obtained.

    def get_test3():
        return 'hellow'
    res = get_test3()
    print(res)
    # return 可以终止函数
  2. Why should there be a return value
    processing function to get the result in the program for further processing.

    have to be aware of is:

    • return function is a sign of the end, there can be multiple return within a function, to return as long as the execution, the function will be executed.

    • return return value can return any data type

    • The number of non-return return value limitations, which can return multiple values ​​separated by commas

      0: return None

      1: The return value is the value itself

      A plurality of: The return value is a tuple

Third, the parameters of the function

And parameter arguments

  1. Parameter
    argument function definition phase in parentheses definition, called the formal parameters, parameter referred to, is essentially the variable name.

    def get_max(num1):
        print(num1)
    #num1  为形参
  2. Arguments
    passed in the function call stage parentheses parameters, known as actual parameters, referred to the argument, in essence, is the value of the variable.

def get_max(num1):
    print(num1)
get_max(8)
#8 为实参

Positional parameters

  1. Location parameter
    in the function definition phase, parameters sequentially from left to right in the order of definition, called the position parameter.

    def get_max(num1,mun2):
     if num1 > num2:
         print(num1)
        else:
            print(num2)
    #默认形参   在函数定义阶段括号内赋值
    #特点:按照位置定义的形参,都必须被传值,多一个不行,少一个也不行。
  2. Position arguments
    in the function call stage, left to right, sequentially defined argument, called argument position.

    get_max(num1=8,mun2=9)

Fourth, the registration function

  1. The user information stored in the file, the user information can be saved asnick:123|sean:456|tank:789

    def get_register():
        #注册
        get_name = input('请输入用户名:')
        get_password = input('请输入密码:')
    
        with open('test.txt','a',encoding='utf8') as f:
            if ':' in get_name:
                print('输入错误,用户名中不能含有":"字符')
            else:
                f.write(f'{get_name}:{get_password}|')
                print('注册成功')
    get_register()

V. login function

  1. User information is read from the file identification

    #登录
    def get_login():
        with open('test.txt','r',encoding='utf8') as fl:
            data = fl.read()
            data_split = data.split(':')
    
        username,password = data_split[0],data_split[1]
        # print(username,password)
        user_name = input('输入用户名:')
        user_password = input('输入密码:')
        if user_name == username and user_password == password:
            print('登陆成功!')
        else:
            print('用户名密码错误!')
    get_login()

Six shopping cart system

Modeled after https://www.cnblogs.com/nickchen121/p/11070005.html  write a shopping cart system (OPTIONAL)

Guess you like

Origin www.cnblogs.com/samoo/p/11448251.html