• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

while单循环练习

python 搞java代码 3年前 (2022-05-21) 38次浏览 已收录 0个评论

单循环实现一行十个★

<code class="language-python"># 方法一
i = 0
<a href="https://www.gaodaima.com/tag/while" title="查看更多关于while的文章" target="_blank">while</a> i < 10:
	<a href="https://www.gaodaima.com/tag/print" title="查看更多关于print的文章" target="_blank">print</a>("★", end="")
	i += 1
print()

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

www#gaodaima.com来源gaodai#ma#com搞*!代#%^码$网搞代码

单循环实现十个换色★

<code class="language-python">i = 1
while i <= 10:
	if i % 2 == 1:
		print("★", end="")
	else:
		print("☆", end="")
	i += 1
print()
</code>

单循环实现十行十列★

<code class="language-python">i = 1
while i <= 100:
	print("★", end="")
	if i % 10 == 0:
		print()
	i += 1
print()
</code>

单循环实现隔列换色★

<code class="language-python">i = 1
while i <= 100:
	if i % 2 == 0:
		print("★", end="")
	else:
		print("☆", end="")
	if i % 10 == 0:
		print()
	i += 1
print()
</code>

单循环实现隔行换色★

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

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

<code class="language-python">i = 1
while i <= 100:
	if i % 2 == 1:
		print(i)
	i += 1
</code>

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

<code class="language-python">i = 1
while i <= 100:
	if i % 2 == 0:
		print(i)
	i += 1
</code>

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

<code class="language-python">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
</code>

折纸求高度

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

<code class="language-python">height = 0.01
times = 1
while True:
	if height * 2 ** times >= 8848:
		print(times)
		break
	times += 1
</code>

篮球弹跳

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

<code class="language-python">times = 1
while True:
	height /= 2
	if times == 10:
		print(height)
		break
	times += 1
</code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:while单循环练习
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址