练习描述
1.计算1-100所有的数之和
2.判断列表中数字2出现的次数
l1 = [11,2,3,2,2,1,2,1,2,3,2,3,2,3,4,3,2,3,2,2,2,2,3,2]
3.编写代码自动生成所有页网址(注意总共多少页)
https://movie.douban.com/top250
4.编写代码打印出下列图形(ps:for循环嵌套)
*****
*****
*****
*****
题解
'''练习1'''
dll = 0
for i in range(1, 101):
dll += i
print(dll) # 5050
print(sum(range(1, 101))) # 5050
'''练习2'''
l1 = [11, 2, 3, 2, 2, 1, 2, 1, 2, 3, 2, 3, 2, 3, 4, 3, 2, 3, 2, 2, 2, 2, 3, 2]
count = 0
for i in l1:
if i == 2:
count += 1
print(count) # 13
'''练习3'''
for i in range(1, 251):
print('https://movie.douban.com/top%s' % (i,)) # 输出250个网页
# 输出豆瓣top250
start_value = 0
page_num = 10
for _ in range(page_num):
url = f'https://movie.douban.com/top250?start={start_value}'
start_value += 25
print(url)
'''练习4'''
for _ in range(4):
for j in range(5):
print('*', end='')
print()
for i in range(4):
print("*****")