while单循环练习


单循环实现一行十个★

# 方法一
i = 0
while i < 10:
	print("★", end="")
	i += 1
print()

# 方法二(通过变量的形式实现)
i = 0
str_var = ""
while i < 10:
	strvar += "★"
	i += 1
print(strvar)

单循环实现十个换色★

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

单循环实现十行十列★

i = 1
while i <= 100:
	print("★", end="")
	if i % 10 == 0:
		print()
	i += 1
print()

单循环实现隔列换色★

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

单循环实现隔行换色★

i = 0
while i < 100:
	if i // 10 % 2 == 0:
		print("★", end="")
	else:
		print("☆", end="")
	if i % 10 == 9:
		print()
	i += 1
print()

单循环输出1-100所有奇数

i = 1
while i <= 100:
	if i % 2 == 1:
		print(i)
	i += 1

单循环输出1-100所有偶数

i = 1
while i <= 100:
	if i % 2 == 0:
		print(i)
	i += 1

单循环实现国际象棋棋盘效果

i = 0
while i < 64:
	# 判断当前是奇数行还是偶数行
	if i // 8 % 2 == 0:
		if i % 2 == 0:
			print("□", end="")
		else:
			print("■", end="")
	else:
		if i % 2 == 0:
			print("■", end="")
		else:
			print("□", end="")
	# 第八个方块换行
	if i % 8 == 7:
		print()
	i += 1

折纸求高度

如题:我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。请问,折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?

height = 0.01
times = 1
while True:
	if height * 2 ** times >= 8848:
		print(times)
		break
	times += 1

篮球弹跳

如题:篮球从十米的位置向下掉落,每一次掉落都是前一次的一半,问弹跳十次之后篮球的高度是多少?

times = 1
while True:
	height /= 2
	if times == 10:
		print(height)
		break
	times += 1