Python循环经典案例(for、while两种方法实现)

循环经典案例(for、while两种方法实现)

  • 1、打印十行十列 *
  • 2、打印十行十列 ★,隔列换色(☆)
  • 3、打印十行十列★,隔行换色
  • 4、99乘法表
  • 5、99乘法表(与题4上下对称)
  • 6、99乘法表(与题4左右对称)
  • 7、99乘法表(与题6上下对称)
  • 8、求吉利数字100~999 (111、222...123、234...987、876...)
  • 9、百钱买百鸡

注:请尽量自行在pycharm完成代码并执行结果!

1、打印十行十列 *

# 方法一:while循环
i = 1
while i <= 10:
	j = 1
	while j <= 10:
		print("*",end="")
		j += 1
	i += 1
	print()

# 方法二:for循环
for i in range(1,11):
	for j in range(1,11):
		print("*",end="")
	print()

2、打印十行十列 ★,隔列换色(☆)

# 方法一:while循环
i = 1
while i <= 10:
	j = 1
	while j <= 10:
		if j % 2 == 1:
			print("★",end="")
		else:
			print("☆",end="")
		j += 1
	i += 1
	print()

# 方法二:for循环
for i in range(1,11):
	for j in range(1,11):
		if j % 2 == 1:
			print("★",end="")
		else:
			print("☆",end="")
	print()

3、打印十行十列★,隔行换色

# 方法一:while循环
i = 1
while i <= 10:
	j = 1
	while j <= 10:
		if i % 2 == 1:
			print("★",end="")
		else:
			print("☆",end="")
		j += 1
	i += 1
	print()

# 方法二:for循环
for i in range(1,11):
	for j in range(1,11):
		if i % 2 == 1:
			print("★",end="")
		else:
			print("☆",end="")
		j += 1
	i += 1
	print()

4、99乘法表

效果图:
Python循环经典案例(for、while两种方法实现)_第1张图片

# 方法一:while循环
i = 1
while i <= 9:
	j = 1
	while j <= i:
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
		j += 1
	i += 1
	print()

# 方法二:for循环
for i in range(1,10):
	for j in range(1,i+1):
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
	print()

5、99乘法表(与题4上下对称)

效果图:
Python循环经典案例(for、while两种方法实现)_第2张图片

# 方法一:while循环
i = 9
while i >= 1:
	j = 1
	while j <= i:
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
		j += 1
	i -= 1
	print()

# 方法二:for循环
for i in range(9,0,-1):
	for j in range(1,i+1):
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
	print()

6、99乘法表(与题4左右对称)

效果图:
Python循环经典案例(for、while两种方法实现)_第3张图片

# 方法一:while循环
i = 1
while i <= 9:
	k = 9 - i
	while k > 0:
		print(7*" ",end="")
		k -= 1
	j = 1
	while j <= i:
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
		j += 1
	i += 1
	print()

# 方法二:for循环
for i in range(1,10):
	for k in range(9-i,0,-1):
		print(7*" ",end="")
	for j in range(1,i+1):
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
	print()		 

7、99乘法表(与题6上下对称)

效果图:
Python循环经典案例(for、while两种方法实现)_第4张图片

# 方法一:while循环
i = 9
while i >= 1:
	k = 9 - i
	while k > 0:
		print(7*" ",end="")
		k -= 1
	j = 1
	while j <= i:
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
		j += 1
	i -= 1
	print()
	
# 方法二:for循环
for i in range(9,0,-1):
	for k in range(9-i,0,-1):
		print(7*" ",end="")
	for j in range(1,i+1):
		num = "%d*%d=%2d " %(i,j,i*j)
		print(num,end="")
	print()		

8、求吉利数字100~999 (111、222…123、234…987、876…)

# 方法一:while循环,运算算法
i = 100
while i <= 999:
	bai = i // 100
	shi = i // 10 % 10
	ge = i % 10
	if bai == shi and shi == ge:
		print(i)
	elif bai+1 == shi and shi+1 == ge:
		print(i)
	elif bai-1 == shi and shi-1 == ge:
		print(i)
	i += 1

# 方法二:while循环,字符串算法
i = 100
while i <= 999:
	bai = i // 100
	shi = i // 10 % 10
	ge = i % 10
	strvar = str(i)
	if strvar[0] == strvar[1] and strvar[1] == strvar[-1]:
		print(i)
	elif bai+1 == shi and shi+1 == ge:
		print(i)
	elif  bai-1 == shi and shi-1 == ge:
		print(i)
	i += 1

# 方法三:for循环
for i in range(100,1000):
	bai = i // 100
	shi = i // 10 % 10
	ge = i % 10
	if bai == shi and shi == ge:
		print(i)
	elif bai+1 == shi and shi+1 == ge:
		print(i)
	elif bai-1 == shi and shi-1 == ge:
		print(i)

9、百钱买百鸡

公鸡 母鸡 小鸡
公鸡1块钱1只,母鸡3块钱一只,小鸡5毛钱一只
问: 用100块钱买100只鸡,有多少种买法?

# 方法一:while循环
count = 0
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:
				count += 1
			z += 1
		y += 1
	x += 1
print(count)

# 方法二:for循环
count = 0
for x in range(0,101):
	for y in range(0,34):
		for z in range(0,101):
			if x+y+z == 100 and x+3*y+0.5*z == 100:
				count += 1
print(count)

你可能感兴趣的:(python)