break,continue练习题
查找第一个回文单词
题目描述
在句子中查找第一个回文单词(正反读都一样)。
运行示例
madam
实现代码
sentence = "hello madam racecar python"
words = sentence.split()for word in words:if word == word[::-1]:print(f"{word}")break
跳过特定数字
题目描述
打印1-20的数字,但跳过所有能被3整除的数。
运行示例
1 2 4 5 7 8 10 11 13 14 16 17 19 20
实现代码
for i in range(1, 21):if i % 3 == 0:continueprint(i,end=" ")
成绩统计
题目描述
输入学生成绩,统计及格人数,遇到-1停止输入,遇到负数或大于100的数跳过。
运行示例
请输入成绩: 85
请输入成绩: 45
请输入成绩: -5
无效成绩,跳过
请输入成绩: 120
无效成绩,跳过
请输入成绩: 92
请输入成绩: -1
及格人数: 2
实现代码
pass_count = 0while True:try:score = int(input("请输入成绩: "))if score == -1:breakelif score < 0 or score > 100:print("无效成绩,跳过")continueelif score >= 60:pass_count += 1except ValueError:print("请输入有效数字")continueprint(f"及格人数: {pass_count}")