sum=0for i inrange(1,101):sum=sum+ i
print('1+2+3+...+100={}'.format(sum))
(2)运行程序,查看结果
二,课后练习(采用for循环打印水仙花数)
(1)编写程序
count =0for n inrange(100,1000):
m = n
p1 = m %10# 个位数
m = m //10
p2 = m %10# 十位数
m = m //10
p3 = m # 百位数if p3**3+ p2**3+ p1**3== n:print('{}^3+{}^3+{}^3={}'.format(p3,p2,p1,n))
count = count +1print('结论:总共有{}个水仙花数'.format(count))
(2)运行程序,查看结果
三,采用for循环输出白遍口号
(1)编写程序
for i inrange(1,101):print('第{}遍:人生苦短,我用python!'.format(i))
(2)运行程序,查看结果
四,阶乘
(一),计算n的阶层
(1)编写程序
a =1
n =int(input('x= '))for i inrange(1,11):
a = a * i
print(a)
(2)运行程序,查看结果
(二),思考计算1!+2!+3!+……+10!的值
(1)编写程序
sum=0for n inrange(1,11):
jc =1for i inrange(1,n+1):
jc = jc * i
sum=sum+ jc
print('1!+2!+3!+...10!={}'.format(sum))
(2)运行程序,查看结果
五,用米粒填充国际象棋棋盘(思考题)
(1)编写程序
sum=0for i inrange(64):sum=sum+2**i
print('{}: {}'.format(i+1,2**i))print('sum = {}'.format(sum))
(2)运行程序,查看结果
六,计算20名学生的平均成绩(防范超出范围的数据)
(1)编写代码
sum=0for i inrange(20):whileTrue:
score =int(input('输入第{}个学生的成绩:'.format(i +1)))if0<= score <=100:breakelse:print('成绩超出范围,必须在[1,100]范围内!')sum+= score
average =sum/20print('平均成绩:{}'.format(round(average,2)))