python经典编程案例——打印九九乘法表
https://blog.csdn.net/cui_yonghua/article/details/95719103
for循环
循环猜年龄
black_gf_age = 24
for i in range(3):
guess = int(input(“猜猜⿊姑娘多⼤了>>:”))
if guess > black_gf_age:
print(“猜的太⼤了,往⼩⾥试试…”)
elif guess < black_gf_age:
print(“猜的太⼩了,往⼤⾥试试…”) else:
exit(“恭喜你,猜对了…”) # 退出程序
break & continue
continue
continue的语法作⽤是, 只要程序⼀遇到continue, 本次循环就不继续了,直接进⼊下⼀次循环
for i in range(1,6):
for j in range(1,9):
if i == 3:
continue # 跳过本次循环,继续下次循环
print(f"{i}层-{i}0{j}室")
break
break的语法作⽤是,只要程序遇到break, 就会结束当前这个循环, 注意如果是多层嵌套循环,只结束当前这⼀层的循环。
for i in range(1, 6):
for j in range(1, 9):
if i == 3:
print(“不⾛3层…”)
continue # 跳过本次循环,继续下次循环
if i == 4 and j == 4: # 遇到404
print(“遇到⻤屋404了,不再继续了”)
break # 结束当前循环, 注意只会结束第2层这个⼩循环。
print(f"{i}层-{i}0{j}室")
while 循环
while 条件: # 只要条件为真, 就会不断的循环
print(xxxxx)
for … else…
for i in range(10):
print(i)
else:
print(“done”) # 循环正常执⾏结束后,会⾛else…
若循环中间被 break了, 则else…不会执⾏
for i in range(10):
print(i)
if i == 5:
break
else:
print(“done”) # 不执⾏
案例
打印99乘法表
For i in range(1,10)
for j in range(1,i➕1)
print(f"{i}✖️{j}={i*j}“,end=”")
print()
求1-100之间的素数
for i in range(2,101):
is_prime_num = true
for j in range(2,i):
if i%j == 0:
is_prime_num = False
if is_prime_num == true:
print(i,“是素数”)
打印三角形
for i in range(11)
if i <= 5:
print(“* " * i)
else:
print(”* " * (10-i))
小球坠落
Height = 100
distince = 0
count = 0
while count <= 10
distince += height
height = height / 2
distance += height
count += 1
print(count,distince,height)
列表中最大值最小值
data = [6,88,44,33,17,9,3]
max_num = data[0]
for i in data:
if i > max_num:
max_num = i
print(max_num)
作业
年会抽三次奖
https://blog.csdn.net/qq_42954795/article/details/127360629