#### 双层循环练习
#十行十列小星星
j &#61;0while j<10:#逻辑代码写在下面
#打印一行十个小星星
i &#61;0while i<10:print("*",end&#61;"")
i&#43;&#61;1
#打印换行
print()
j&#43;&#61;1
#十行十列隔列换色小星星
j &#61;0while j<10:#打印星星
i&#61;0while i<10:if i % 2 &#61;&#61;0:print("★",end&#61;"")else:print("☆",end&#61;"")
i&#43;&#61;1
#打印换行
print()
j&#43;&#61;1
#十行十列隔行换色小星星
"""j动的慢,i动的快,外面循环动一次,里面循环动10次"""j&#61;0while j<10:#打印星星
i&#61;0while i<10:if j % 2 &#61;&#61;0:print("★",end&#61;"")else:print("☆",end&#61;"")
i&#43;&#61;1
#打印换行
print()
j&#43;&#61;1
"""j &#61; 0 j % 2 ★★★★★★★★★★
j &#61; 1 j % 2 ☆☆☆☆☆☆☆☆☆☆
...
依次类推"""
#99乘法表
"""&#39;%d*%d&#61;%2d&#39; % (值1,值2,值3) 字符串的格式化"""
#方向一 (正序 1~9)
i &#61; 1
while i<&#61;9:
j&#61; 1
while j<&#61;i:#print(i,j)
#打印对应的表达式
print("%d*%d&#61;%2d" % (i,j,i*j) , end&#61;"")
j&#43;&#61;1
#打印换行
print()
i&#43;&#61;1
#方向二 (倒序 9~1)
print("<&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>")
i&#61; 9
while i>&#61;1:
j&#61; 1
while j<&#61;i:#print(i,j)
#打印对应的表达式
print("%d*%d&#61;%2d" % (i,j,i*j) , end&#61;"")
j&#43;&#61;1
#打印换行
print()
i-&#61;1
print(",&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;.")#方向三
i &#61; 1
while i<&#61;9:#(1)打印空格 (8 ~ 1)
k &#61; 9 -iwhile k>0:print(" ",end&#61;"")
k-&#61;1
#(2)打印星星
j &#61; 1
while j<&#61;i:#print(i,j)
#打印对应的表达式
print("%d*%d&#61;%2d" % (i,j,i*j) , end&#61;"")
j&#43;&#61;1
#(3)打印换行
print()
i&#43;&#61;1
print("<&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>")#方向四
i &#61; 9
while i>&#61;1:#(1)打印空格 (8 ~ 1)
k &#61; 9 -iwhile k>0:print(" ",end&#61;"")
k-&#61;1
#(2)打印星星
j &#61; 1
while j<&#61;i:#print(i,j)
#打印对应的表达式
print("%d*%d&#61;%2d" % (i,j,i*j) , end&#61;"")
j&#43;&#61;1
#(3)打印换行
print()
i-&#61;1
#求吉利数字100 ~ 999 123 321 111 222 333 ... 666 888 567 765
"""765
// 可以取到一个数高位
% 可以取到一个数低位
个位: 765 % 10 &#61;> 5
十位: 765 // 10 % 10 &#61;> 6
百位: 765 // 100 &#61;> 7"""
#方法一:
i &#61; 100
while i<&#61;999:#个位
gewei &#61; i % 10
#十位
shiwei &#61; i // 10 % 10
#百位
baiwei &#61; i // 100
#print(baiwei,shiwei,gewei)
#555 666 777
if shiwei &#61;&#61; gewei and shiwei &#61;&#61;baiwei:print(i)#567 789
elif shiwei &#61;&#61; gewei - 1 and shiwei &#61;&#61; baiwei &#43; 1:print(i)elif shiwei &#61;&#61; gewei &#43; 1 and shiwei &#61;&#61; baiwei - 1:print(i)
i&#43;&#61;1
#方法二
print("<&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>")"""strvar &#61; "789"
strvar[0]
strvar[1]
strvar[-1]"""i&#61; 100
while i<&#61;999:
num&#61;str(i)#个位
gewei &#61; int(num[-1])#十位
shiwei &#61; int(num[-2])#百位
baiwei &#61; int(num[-3])#555 666 777
if shiwei &#61;&#61; gewei and shiwei &#61;&#61;baiwei:print(i)#567 789
elif shiwei &#61;&#61; gewei - 1 and shiwei &#61;&#61; baiwei &#43; 1:print(i)#765
elif shiwei &#61;&#61; gewei &#43; 1 and shiwei &#61;&#61; baiwei - 1:print(i)
i&#43;&#61;1
#百钱买百鸡 公鸡,母鸡,小鸡,公鸡1块钱一只,母鸡3块钱一只,小鸡是5毛钱一只,问100块钱买100只鸡,有多少种买法
"""穷举法:一个一个试
a &#61; [1,2]
b &#61; [3,4]
c &#61; [5,6]
a&#43;b&#43;c &#61; 10?
1 &#43; 3 &#43; 5 &#61; 9
1 &#43; 3 &#43; 6 &#61; 10 ok
1 &#43; 4 &#43; 5 &#61; 10 ok
1 &#43; 4 &#43; 6 &#61; 11
2 &#43; 3 &#43; 5 &#61; 10 ok
2 &#43; 3 &#43; 6 &#61; 11
2 &#43; 4 &#43; 5 &#61; 11
2 &#43; 4 &#43; 6 &#61; 12
公鸡 : x
母鸡 : y
小鸡 : z
# 100只
x&#43;y&#43;z &#61; 100
# 100块
x&#43;3y&#43;0.5z &#61; 100
通过and 把两个条件拼接在一起"""x&#61;0
count&#61;0while x<&#61;100:
y&#61;0while y<&#61;33:
z&#61;0while z<&#61;100:if (x&#43;y&#43;z &#61;&#61; 100) and (x&#43;3*y&#43;0.5*z &#61;&#61; 100):
count&#43;&#61;1
print(x,y,z)
z&#43;&#61;1y&#43;&#61;1x&#43;&#61;1
print(count)