- while循环
1.1实现0-100相加总数
i = 1
sum = 0
While i <= 100:
Sum += i
i + = 1
Print(sum)
1.2实现0-100三的倍数相加和个位十位上含三的数相加
i = 1
Sum = 0
While i <= 100:
If i % 3 == 0 and i // 3 ==0 and i % 10 == 3
Sum += i
i +=1
Print(sum)
2.break终止循环和continue回到循环开始处
2.1练习题
我一共有四个苹果,但是我在吃到第三苹果以后就已经吃饱了,吃不下第四个了
i = 1
While i <= 4:
if i == 3:
Print(f“第{i}个我吃饱了”)
Break
i +=1
2.2依旧是四个苹果当我吃到第三个苹果的时候有毛毛虫,第
三个苹果被我丢了,继续吃第四个苹果
i = 1
While i < 4:
j+=1
If i == 3:
Print(f”第{i}苹果被毛毛虫吃了”)
Continue
Print(f“吃第{i}个苹果”)
# break 和continue区别 break 是终止所有循环,continue是跳出本次循环回到循环开始处
3.for循环
使用for循环打印出python每个英文字母
Str1 = “python”
for i in str1:
Print(i)
3.1使用while循环取出y和0.并打印出来
Str1= “python”
i = 0
While i < len(str1):
If i ==1 or i == 4:
Print(str2[i])
i += 1
3.2使用for循环打印James并且跳过字母m
Str3 = “james”
For i in str3:
If i == “m”:
Continue
Print(i, end=” ”)
4.猜拳游戏
'''
猜拳游戏
石头(1)剪刀(2) 布(3)
电脑赢:1-2,2-3,3-1
平局:1-1 2-2 3-3
我赢:1-3,2-1,3-2
'''
# import random(参数)随机导入