实例学习while循环:
1、一共5个苹果,吃到第4个时吃饱了,不吃了。
#1、一共5个苹果,吃到第4个时吃饱了,不吃了。
# i = 5
# while i<1:
# i = i-1
# break
# print("吃饱了,不吃了")
i =1
while i <= 5:
if i ==4:
print(f'吃饱了不能再吃了')
break
print(f'我已经吃到第{i}个了')
i = i+1
2、一共5个苹果,吃到第3个时发现一只大虫子,第3个不吃了,继续吃第4个苹果,直到吃完
# #2、一共5个苹果,吃到第3个时发现一只大虫子,第3个不吃了,继续吃第4个苹果,直到吃完
i = 1
while i<=5:
if i==3:
print("有一只大虫子,不吃第三个了")
i = i+1
continue
print ( f'我已经吃到第{i}个苹果了')
i =i+1
3、打印:
***** ***** ***** ***** *****
# #3、案例打印:
# *****
# *****
# *****
# *****
# *****
j=1
while j<=5:
i = 1
while i<=5:
print('*',end="")
i=i+1
print()
j=j+1
4、 打印:
* ** *** **** *****
# #4、案例打印:
# *
# **
# ***
# ****
# *****
j=1
while j<=5:
i = 1
while i<=j:
print('*',end="")
i=i+1
print()
j=j+1
5、 打印乘法口诀
# #5、打印乘法口诀
j=1
while j<=9:
i = 1
while i<=j:
print(f'{i}*{j}={i*j}',end="\t")
i=i+1
print()
j=j+1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)