1. 基础题
-
输入年份,如果输入的年是闰年打印
'闰年'否则打印'平年'year = int(input('输入年份')) if year % 400 == 0 or year % 4 == 0 and year % 100 != 0: print('闰年') else: print('平年') -
使用for和while循环输出 0~100内所有3的倍数。
# for for i in range(3, 100, 3): print(i) # while i=3 while i < 100: print(i) i += 3 -
使用for和while循环输出100~200内个位数或者十位数能被3整除的数。
# for for i in range(100, 200): single_digit = i % 10 ten_digits = i // 10 % 10 if single_digit % 3 == 0 or ten_digits % 3 == 0: print(i) # while i = 100 while i < 200: single_digit = i % 10 ten_digits = i // 10 % 10 if single_digit % 3 == 0 or ten_digits % 3 == 0: print(i) i += 1 -
使用for和while循环统计100~200中十位数是5的数的个数
# for count = 0 for i in range(100, 200): ten_digits = i // 10 % 10 if ten_digits == 5: count += 1 print(count) # while i = 100 count = 0 while i < 200: ten_digits = i // 10 % 10 if ten_digits == 5: count += 1 i += 1 print(count) -
使用for和while循环打印50

这篇博客介绍了使用Python中的for和while循环进行一系列编程练习,从基础题到进阶题再到挑战题。基础题包括判断闰年、输出3的倍数等;进阶题涉及计算特定条件的数的个数和水仙花数;挑战题涵盖了素数判断、斐波那契数列、99乘法表和经典问题‘百马百担’的解决方案。通过这些题目,读者可以提升对循环控制结构的理解和应用能力。
最低0.47元/天 解锁文章
2357

被折叠的 条评论
为什么被折叠?



