作业2.4

138 阅读1分钟
#-*- codeing = utf -8 -*- 
#@Time:
#@Author:番茄
#@File:shuzihuobi--2.4.py
#@Software:PyCharm

# 函数(方法)定义

"""
def 函数名称 (参数1,参数2):
    程序语句
"""

# def print_hello():
#     print("hello world")


# 函数调用 (通过函数名加括号调用)

# print_hello()

# 带有参数的函数

# def print_hello1(name,age,score):
#     print("hello world",name,age,score)
#
# print_hello1("stephen") # 带参数的函数

# def print_hello2(name,age,score):
#     print("hello world",name,age,score)
#
# print_hello2("stephen",20,80) # 把各种属性打出来

# def child_or_adult(age):
#     if age > 18 :
#         return "adult"
#     else:
#         return "child"
#
# per = child_or_adult(15)
# print(per)
#
# def child_or_adult(age):
#     if age > 18 :
#         return "adult"
#     else:
#         return "child"
#
# per = child_or_adult(19)
# print(per)

# 局部变量 和 全局变量

# num = 100 # 全局变量
#
# def func():
#     num=123 #局部变量
#     print(num)
#
# func()


# num = 100
#
# def func():
#     num += 100 # num = num + 100
#     print(num)
#
# func()

# 函数内部的变量名 如果第一次出现,且出现在 =前面,即被

# 函数内部的变量如果第一次出现,且出现在=前面,即被视为定义一个局部变量。

# 函数内部的变量名如果第一次出现,且出现在=后面,且该变量在全局域中已定义,则

# 在这里将引用全局变量,如果该变量在全局域中没有定义,当然会出现”变量未定义“的错误,例如:

# num = 100
# def func():
#
#     x = num + 100
#     print(x)
#
# func()
#
# num = 100

#
# def func():
#     num =200
#     x = num + 100
#     print(x)
#
#
# func()

num = 100

def func():
    global num  # 添加这个关键词 global 就等于把抬头的 num 给全局了
    num = 200
    num += 100
    print(num)

func()
print(num)

# 直接 main

if __name__ == '__main__':  # 直接打 main
    pass
    # 面向对象编程
    # self
# pip install 安装

# 一些学习网站

# 网易云课堂
# imooc
# 极客网