🌌 专注Golang,Python语言,云原生,人工智能领域得博主; 💜 过去经历的意义在于引导你,而非定义你; 📢 欢迎点赞 👍 收藏 ?留言!
while循环
i = 0
while i < 5:
print('操场跑圈中.....')
i += 1
print('跑圈完成')
while实现猜拳游戏
import random
while True:
user = int(input('请输入要出的拳:1(石头) 2(剪刀) 3(布):'))
computer = random.randint(1, 3)
if user == computer:
print('平局')
elif (user == 1 and computer == 2) or (user == 2 and computer == 3) or (user == 3 and computer == 1):
print('恭喜你,胜利了')
else:
print('你输了,弱爆了')
计算1-100之间的累加和
my_sum = 0
i = 1
while i <= 100:
my_sum += i
i += 1
print('求和的结果为', my_sum)
计算1-100之间偶数的累加和
my_sum = 0
i = 1
while i <= 100:
if i % 2 == 0:
my_sum += i
i += 1
print('求和的结果为', my_sum)
计算1-100之间偶数的累加和2
my_sum = 0
i = 2
while i <= 100:
my_sum += i
i += 2
print('求和的结果为', my_sum)
while循环嵌套
i = 0
while i < 5:
j = 0
print('操场跑圈中.....')
while j < 3:
print('做了一个俯卧撑')
j += 1
i += 1
for循环
for i in 'hello':
print(i, end=' ')
for i in range(5):
print('操场跑圈...')
for i in range(3, 7):
print(i)
for i in range(1, 10, 3):
print(i)
for 循环嵌套
for i in range(5):
print('操场跑圈中')
for j in range(3):
print('做一个俯卧撑....')
循环嵌套使用-打印正方形
n = int(input('请输入正方形的边长:'))
j = 0
while j < n:
i = 0
while i < n:
print('*', end=' ')
i += 1
print()
j += 1
for j in range(n):
for i in range(n):
print('*', end=' ')
print()
打印三角形
n = 5
j = 1
while j <= n:
i = 1
while i <= j:
print('*', end=' ')
i += 1
print()
j += 1
for i in range(n):
for j in range(i+1):
print('*', end=' ')
print()
break
for i in range(1, 6):
if i == 4:
print('吃饱了, 不吃了')
break
print(f'正在吃标号为 {i} 的苹果')
continue
for i in range(1, 6):
if i == 4:
print('发现半条虫子,这个苹果不吃了, 没吃饱,继续吃剩下的')
continue
print(f'吃了编号为{i}的苹果')
循环和else
my_str = 'hello python!'
for i in my_str:
if i == 'p':
print('包含p这个字符')
break
else:
print('不包含p这个字符')
|