python学习4——小说,4fiction


# fiction函数
# def 定义

def say_hi():
    print("hi!")
# 我们需要在函数体内用缩进,用tab键

say_hi()
say_hi()

def print_sum_two(a, b):
    c=a+b
    print(c)
# 无返回值打印
# 传入具体参数
print_sum_two(3,6)

def hello_some(str):
    print("Hello "+str+"!")

hello_some("world")
hello_some("China")

# 有返回值的例子
def repeat_str(str, times):
    repeated_str = str * times
    return repeated_str

repeated_string =repeat_str("happy birthday! ",5)
print(repeated_string )

#全局变量
x=60

def foo(x):
    print("x is: ",str(x))
    x = 3  # 局部变量
    print("change local x to "+str(x))

foo(x)
print("x is still "+str(x))

y=60
# 在函数内部声明全局变量
def foo2(x):
    global y
    print("y is: ",str(y))
    y = 3  # 局部变量
    print("change local x to "+str(y))

foo2(y)
print("y is still "+str(y))