作业讲解

  1. 写段代码。计算 1-100 之积 ```python res = 1 for i in range(1,101): res = res*i

print(res)

  1. 2. 数学中斐波那契数列0 11235,初始值为01,后面每个值都为前面两个数之和,求100以内符合条件的斐波那契数列;
  2. 100个斐波那契数列
  3. ```python
  4. a,b = 0,1
  5. for i in range(100):
  6. print(a, end=',')
  7. a,b=b,b+a
  1. a,b = 0,1
  2. while True:
  3. if a > 100:
  4. break
  5. print(a,end=',')
  6. a,b = b, a+b
  1. 数字 121,数字从左往右看与从右往左看 值相等,我们称这个数字为回文数,比如121,131, 写段程序,判断某个数字是否为回文数。
  1. a = input("请输入数字:")
  2. if a == a[::-1]:
  3. print(f'{a} == {a[::-1]}, {a}是回文数')
  4. else:
  5. print(f'{a} !== {a[::-1]}, {a} 不是回文数')
  1. 写段程序,打印九九乘法表
    1. for x in range(1,10):
    2. for y in range(x):
    3. print(f'{y+1}*{x}={(y+1)*x}',end='\t')
    4. print()
    打印金子塔结构
    1. for x in range(1,11):
    2. print(' '*(11-(x+1)) + ((2*x)-1) * '*')
    ```python
    1. *
    2. ***
    3. *****







  1. ```python
  2. # s = "*".center(19)
  3. # print(s)
  4. for i in range(10):
  5. count = 2*10-1
  6. f = "*"* (2*(i+1)-1)
  7. s = f.center(count)
  8. print(s)
  1. 设计程序,提示用户输入月份1-12, 根据用户输入的数字,打印对应的季节,比如用户输入2,打印出春季.
    1. month = input('请输入月份:')
    2. if month in ['1','2','3']:
    3. print('春季')
    4. elif month in ['4','5','6']:
    5. print('夏季')
    6. elif month in ['7','8','9']:
    7. print('秋季')
    8. elif month in ['10','11','12']:
    9. print('冬季')
    10. else:
    11. print('输入有误')

continue

表示继续从头开始,

与break跳出循环不一样

  1. for x in range(10):
  2. print(f'x={x}')
  3. if x == 3 or x == 6:
  4. break
  1. x=0
  2. x=1
  3. x=2
  4. x=3

遇到continue 的时候,会从开始循环处继续循环,不会中断循环,但是continue下面的语句不再执行;
image.png

  1. for x in range(10):
  2. print(f'before: x={x}')
  3. if x == 3 or x == 6:
  4. continue
  5. print(f'end: x={x}')
  1. before: x=0
  2. end: x=0
  3. before: x=1
  4. end: x=1
  5. before: x=2
  6. end: x=2
  7. before: x=3
  8. before: x=4
  9. end: x=4
  10. before: x=5
  11. end: x=5
  12. before: x=6
  13. before: x=7
  14. end: x=7
  15. before: x=8
  16. end: x=8
  17. before: x=9
  18. end: x=9
  19. Process finished with exit code 0
  1. couter = 0
  2. while couter < 10:
  3. couter += 1
  4. if couter % 2 == 0:
  5. continue
  6. print(couter)