作业讲解
- 写段代码。计算 1-100 之积 ```python res = 1 for i in range(1,101): res = res*i
print(res)
2. 数学中斐波那契数列0, 1,1,2,3,5,初始值为0,1,后面每个值都为前面两个数之和,求100以内符合条件的斐波那契数列;
100个斐波那契数列
```python
a,b = 0,1
for i in range(100):
print(a, end=',')
a,b=b,b+a
a,b = 0,1
while True:
if a > 100:
break
print(a,end=',')
a,b = b, a+b
- 数字 121,数字从左往右看与从右往左看 值相等,我们称这个数字为回文数,比如121,131, 写段程序,判断某个数字是否为回文数。
a = input("请输入数字:")
if a == a[::-1]:
print(f'{a} == {a[::-1]}, {a}是回文数')
else:
print(f'{a} !== {a[::-1]}, {a} 不是回文数')
- 写段程序,打印九九乘法表
打印金子塔结构for x in range(1,10):
for y in range(x):
print(f'{y+1}*{x}={(y+1)*x}',end='\t')
print()
```pythonfor x in range(1,11):
print(' '*(11-(x+1)) + ((2*x)-1) * '*')
*
***
*****
```python
# s = "*".center(19)
# print(s)
for i in range(10):
count = 2*10-1
f = "*"* (2*(i+1)-1)
s = f.center(count)
print(s)
- 设计程序,提示用户输入月份1-12, 根据用户输入的数字,打印对应的季节,比如用户输入2,打印出春季.
month = input('请输入月份:')
if month in ['1','2','3']:
print('春季')
elif month in ['4','5','6']:
print('夏季')
elif month in ['7','8','9']:
print('秋季')
elif month in ['10','11','12']:
print('冬季')
else:
print('输入有误')
continue
表示继续从头开始,
与break跳出循环不一样
for x in range(10):
print(f'x={x}')
if x == 3 or x == 6:
break
x=0
x=1
x=2
x=3
遇到continue 的时候,会从开始循环处继续循环,不会中断循环,但是continue下面的语句不再执行;
for x in range(10):
print(f'before: x={x}')
if x == 3 or x == 6:
continue
print(f'end: x={x}')
before: x=0
end: x=0
before: x=1
end: x=1
before: x=2
end: x=2
before: x=3
before: x=4
end: x=4
before: x=5
end: x=5
before: x=6
before: x=7
end: x=7
before: x=8
end: x=8
before: x=9
end: x=9
Process finished with exit code 0
couter = 0
while couter < 10:
couter += 1
if couter % 2 == 0:
continue
print(couter)