一、循环嵌套

1、循环嵌套小星星
① print函数须知
 #每一行要打印的星星就是和当前的行数是一致的 #增加一个小的循环,专门负责当前行中,每一‘列 ‘的星星显示
i = 1
while i <= 5:
j = 1
while j <= i:
print("*", end="")
j += 1
print("第 %d 行" % i)
i += 1

i = 1
while i <= 5:
j = 1
while j <= i:
print("*", end="")
j += 1
print("")
i += 1

2、循环嵌套九九乘法表
\t在控制台输出一个制表符,协助在输出文本时垂直方向保持对齐
\n在控制台输出一个换行符
i = 1
while i <= 9:
j = 1
while j <= i:
print("%d * %d = %d" % (j, i, i * j), end="\t")
j += 1
print("")
i += 1

二、函数

定义函数:(内容要tab) 新建项目:import导入并调用函数 
1、函数的基本使用
01函数的定义语法

02函数的定义和演练
name = "欢子"
def say_hello():
print("hello 1")
print("hello 2")
print("hello 3")
print(name)
say_hello()
print(name)
 
2、函数参数的使用

def sum_2_sum(num1, num2):
result = num1 + num2
print("%d + %d = %d" % (num1, num2, result))
sum_2_sum(50, 20)

3、函数的返回值

def sum_2_sum(num1, num2):
result = num1 + num2
return result
sum_result = sum_2_sum(50, 20)
print("计算结果:%d" % sum_result)

4、函数的嵌套使用
def test1():
print("*" * 50)
def test2():
print("@" * 50)
test1()
print("+" * 50)
test2()

5、函数的分割线演练
def print_line(char, times):
print(char * times)
def print_lines():
row = 0
while (row < 5):
print_line("-", 50)
row += 1
print_lines()
二次调用:
def print_line(char, times):
print(char * times)
def print_lines(char, times):
row = 0
while (row < 5):
print_line(char, times)
row += 1
print_lines("-", 50)

|