900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python 双层for循环_day05-Python运维开发基础(双层循环 pass/break/continue for循环)...

python 双层for循环_day05-Python运维开发基础(双层循环 pass/break/continue for循环)...

时间:2020-04-08 19:14:38

相关推荐

python 双层for循环_day05-Python运维开发基础(双层循环 pass/break/continue for循环)...

#### 双层循环练习

#十行十列小星星

j =0while j<10:#逻辑代码写在下面

#打印一行十个小星星

i =0while i<10:print("*",end="")

i+=1

#打印换行

print()

j+=1

#十行十列隔列换色小星星

j =0while j<10:#打印星星

i=0while i<10:if i % 2 ==0:print("★",end="")else:print("☆",end="")

i+=1

#打印换行

print()

j+=1

#十行十列隔行换色小星星

"""j动的慢,i动的快,外面循环动一次,里面循环动10次"""j=0while j<10:#打印星星

i=0while i<10:if j % 2 ==0:print("★",end="")else:print("☆",end="")

i+=1

#打印换行

print()

j+=1

"""j = 0 j % 2 ★★★★★★★★★★

j = 1 j % 2 ☆☆☆☆☆☆☆☆☆☆

...

依次类推"""

#99乘法表

"""'%d*%d=%2d' % (值1,值2,值3) 字符串的格式化"""

#方向一 (正序 1~9)

i = 1

while i<=9:

j= 1

while j<=i:#print(i,j)

#打印对应的表达式

print("%d*%d=%2d" % (i,j,i*j) , end="")

j+=1

#打印换行

print()

i+=1

#方向二 (倒序 9~1)

print("<============>")

i= 9

while i>=1:

j= 1

while j<=i:#print(i,j)

#打印对应的表达式

print("%d*%d=%2d" % (i,j,i*j) , end="")

j+=1

#打印换行

print()

i-=1

print(",=============.")#方向三

i = 1

while i<=9:#(1)打印空格 (8 ~ 1)

k = 9 -iwhile k>0:print(" ",end="")

k-=1

#(2)打印星星

j = 1

while j<=i:#print(i,j)

#打印对应的表达式

print("%d*%d=%2d" % (i,j,i*j) , end="")

j+=1

#(3)打印换行

print()

i+=1

print("<====================>")#方向四

i = 9

while i>=1:#(1)打印空格 (8 ~ 1)

k = 9 -iwhile k>0:print(" ",end="")

k-=1

#(2)打印星星

j = 1

while j<=i:#print(i,j)

#打印对应的表达式

print("%d*%d=%2d" % (i,j,i*j) , end="")

j+=1

#(3)打印换行

print()

i-=1

#求吉利数字100 ~ 999 123 321 111 222 333 ... 666 888 567 765

"""765

// 可以取到一个数高位

% 可以取到一个数低位

个位: 765 % 10 => 5

十位: 765 // 10 % 10 => 6

百位: 765 // 100 => 7"""

#方法一:

i = 100

while i<=999:#个位

gewei = i % 10

#十位

shiwei = i // 10 % 10

#百位

baiwei = i // 100

#print(baiwei,shiwei,gewei)

#555 666 777

if shiwei == gewei and shiwei ==baiwei:print(i)#567 789

elif shiwei == gewei - 1 and shiwei == baiwei + 1:print(i)elif shiwei == gewei + 1 and shiwei == baiwei - 1:print(i)

i+=1

#方法二

print("<=====================>")"""strvar = "789"

strvar[0]

strvar[1]

strvar[-1]"""i= 100

while i<=999:

num=str(i)#个位

gewei = int(num[-1])#十位

shiwei = int(num[-2])#百位

baiwei = int(num[-3])#555 666 777

if shiwei == gewei and shiwei ==baiwei:print(i)#567 789

elif shiwei == gewei - 1 and shiwei == baiwei + 1:print(i)#765

elif shiwei == gewei + 1 and shiwei == baiwei - 1:print(i)

i+=1

#百钱买百鸡 公鸡,母鸡,小鸡,公鸡1块钱一只,母鸡3块钱一只,小鸡是5毛钱一只,问100块钱买100只鸡,有多少种买法

"""穷举法:一个一个试

a = [1,2]

b = [3,4]

c = [5,6]

a+b+c = 10?

1 + 3 + 5 = 9

1 + 3 + 6 = 10 ok

1 + 4 + 5 = 10 ok

1 + 4 + 6 = 11

2 + 3 + 5 = 10 ok

2 + 3 + 6 = 11

2 + 4 + 5 = 11

2 + 4 + 6 = 12

公鸡 : x

母鸡 : y

小鸡 : z

# 100只

x+y+z = 100

# 100块

x+3y+0.5z = 100

通过and 把两个条件拼接在一起"""x=0

count=0while x<=100:

y=0while y<=33:

z=0while z<=100:if (x+y+z == 100) and (x+3*y+0.5*z == 100):

count+=1

print(x,y,z)

z+=1y+=1x+=1

print(count)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。