循环练习
Posted bling-bling-star
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环练习相关的知识,希望对你有一定的参考价值。
# 双层while 写 10行10列的@
i = 0
while i < 10:
j = 0
while j<10:
print("@",end="")
j+=1
print()
i+=1
# 双层 while 写 10行10列的隔行,(或隔列) 换色的●○
i = 0
while i <10:
j = 0
while j<10:
if i % 2 == 0:
print("●",end="")
else:
print("○",end="")
j+=1
print()
i+=1
# ( 或隔列 )
i = 0
while i <10:
j = 0
while j<10:
if j % 2 == 0:
print("●",end="")
else:
print("○",end="")
j+=1
print()
i+=1
# 关于字符串的格式化 %d %f %s
# %d 用来表示一个占位符 用来替换整形
# %f 用来表示一个占位符 用来替换浮点形
# %s 用来表示一个占位符 用来替换字符串
# res = 123
"""
%2d 是%d 占了2位 默认右对齐
如果加了- 默认左对齐
"""
"""
res = "%d"%(123)
res = "%d%d%d"%(12,13,14)
res = "%d"%(3.99) # 小数点舍弃
res = "%-3d"%(8)
print(res)
res2 = "%3d"%(12)
print(res2)
# %f 存在小数进制问题
res = "%f"%(3)
res = "%.1f"%(3)
print(res)
res = "%s"%("撒地方")
print(res)
res = "%s"%("today weather good good o")
print(res)
res = "---%d-----%s----%.1f"%(345,"沙发",3.999)
print(res)
"""
# 99乘法表 (形状一)
""""""
i = 1
while i<=9:
j = 1
while j<=i:
print("%d*%d=%2d "%(i,j,i*j),end="")
j+=1
print()
i+=1
print("<=================================>")
i = 9
while i>0:
j = 1
while j<=i:
print("%d*%d=%2d "%(i,j,i*j),end="")
j+=1
print()
i-=1
print("<===>")
i = 1
while i<=9:
# 针对于空格
k = 9-i
while k>0:
print(" ",end="")
k-=1
j = 1
while j<=i:
print("%d*%d=%2d "%(i,j,i*j),end="")
j+=1
print()
i+=1
print("<====>")
i = 9
while i>0:
# 针对于空格
k = 9-i
while k>0:
print(" ",end="")
k-=1
j = 1
while j<=i:
print("%d*%d=%2d "%(i,j,i*j),end="")
j+=1
print()
i-=1
# 吉利数字: 666 888 999 123 321 100 ~ 999 找出来
#257 n // 10 % 10
# 方法一
i = 100
while i<=999:
gewei = i % 10
shiwei = i // 10 % 10
baiwei = i // 10 // 10
if (shiwei == gewei) and (shiwei == baiwei) :
print(i)
if (shiwei == gewei + 1) and (shiwei == baiwei - 1):
print(i)
if (shiwei == gewei - 1) and (shiwei == baiwei + 1):
print(i)
i+=1
#方法二
print("<====>")
i = 100
while i<=999:
strvar = str(i)
baiwei = int(strvar[0])
shiwei = int(strvar[1])
gewei = int(strvar[2])
if (shiwei == gewei) and (shiwei == baiwei):
print(i)
if (shiwei == gewei + 1) and (shiwei == baiwei - 1):
print(i)
if (shiwei == gewei - 1) and (shiwei == baiwei + 1):
print(i)
i+=1
"""
百钱买白鸡
百钱白鸡: 公鸡 = 1 母鸡 = 3 小鸡 = 0.5 100块钱 买 100只鸡 可以有多少种买法
"""
"""
x+y+z = 100
x + 3y + 0.5z = 100
"""
# x,y,z 分别代表公鸡,母鸡,小鸡 公鸡最多买100只鸡,母鸡最大33只,小鸡 最多100鸡
x = 0
while x<=100:
y=0
while y<=33:
z=0
while z<=100:
if (x+y+z == 100) and (x+3*y+0.5*z == 100):
print(x,y,z)
z+=1
y+=1
x+=1
以上是关于循环练习的主要内容,如果未能解决你的问题,请参考以下文章