python程序设计基础3:python选择和循环结构
2014-03-27 15:59
543 查看
这一节的知识点很简单:选择:if else 循环:while或者for。所以这一节我们直接上例题。
1.输入三角形的三条边,判断是否能组成三角形。若是可以,计算三角形的面积。
代码:
import math
a,b,c=input('please input 3 numbers(with ","in them):')
if a+b>c and a+c>b and b+c>a:
print 'this is a triangle !'
p=(a+b+c)/2
areaspr=p*(p-a)*(p-b)*(p-c)
area=math.sqrt(areaspr)
print 'area=',area
else:
print'that is not triangle!'
结果:
please input 3 numbers(with ","in them):3,4,5
this is a triangle !
area= 6.0
please input 3 numbers(with ","in them):1,2,6
that is not triangle!
2.循环 主要使用while和 for实现
1、询问用户输入一个成绩,计算平均成绩输出,并询问是否继续。
代码:
sumall=0.0
num=0
endflag='y'
while endflag=='y':
score=input('please input a score!')
num=num+1
sumall=sumall+score
mean=sumall/num
print'the mean is:',mean
endflag=raw_input('would you like to go on?(y/n)')
结果:
please input a score!89
the mean is: 89.0
would you like to go on?(y/n)y
please input a score!87
the mean is: 88.0
would you like to go on?(y/n)y
please input a score!68
the mean is: 81.3333333333
would you like to go on?(y/n)n
2、输入一个列表,输出里面所有偶数元素的和
代码:
num_list=input('please input a list:')
sum=0
for num in num_list:
if num%2==0:
sum=sum+num
print 'the sum of even number is:',sum
结果:
please input a list:[0,4,3,5,7,8]
the sum of even number is: 12
3、while解决不确定次数的循环:从键盘输入若干整数,求所有输入的正数的和,遇到负数以后便结束该操作。
代码:
sum=0
num=input('please input a number,a negtive means the end:')
while num>0:
sum=sum+num
num=input('please input a number,a negtive means the end:')
print'the sum of positive is :',sum
print'the end!'
结果:
please input a number,a negtive means the end:4
please input a number,a negtive means the end:5
please input a number,a negtive means the end:7
please input a number,a negtive means the end:2
please input a number,a negtive means the end:-1
the sum of positive is : 18
the end!
4、计算s=1+.....+1/n!
代码:
num=input('please input a number:')
sum=0.0
sumall=0.0
n=1
while n<=num:
sum=sum+n
sumall=sumall+1/sum
n=n+1
print 'the addition is:',sumall
结果:
please input a number:11
the addition is: 1.83333333333
5、求出0-100中能被7整除,但是不能被5整除的所有整数。
for i in range(101):
if i%7==0 and i%5 !=0:
print i
结果:
7
14
21
28
42
49
56
63
77
84
91
98
6、使用FOR循环输出9*9乘法表:
代码:
for i in range(1,10):
for j in range(i,10,1):
print i,'*',j,'=',i*j,'\t',
print '\n'
结果:
1 * 1 = 1 1 * 2 = 2
1 * 3 = 3 1 * 4 = 4
1 * 5 = 5 1 * 6 = 6
1 * 7 = 7 1 * 8 = 8
1 * 9 = 9
2 * 2 = 4 2 * 3 = 6
2 * 4 = 8 2 * 5 = 10
2 * 6 = 12 2 * 7 = 14
2 * 8 = 16 2 * 9 = 18
3 * 3 = 9 3 * 4 = 12
3 * 5 = 15 3 * 6 = 18
3 * 7 = 21 3 * 8 = 24
3 * 9 = 27
4 * 4 = 16 4 * 5 = 20
4 * 6 = 24 4 * 7 = 28
4 * 8 = 32 4 * 9 = 36
5 * 5 = 25 5 * 6 = 30
5 * 7 = 35 5 * 8 = 40
5 * 9 = 45
6 * 6 = 36 6 * 7 = 42
6 * 8 = 48 6 * 9 = 54
7 * 7 = 49 7 * 8 = 56
7 * 9 = 63
8 * 8 = 64 8 * 9 = 72
9 * 9 = 81
上面的print i,'*',j,'=',i*j,'\t', 中最后一个,很重要
7、求200以内能够被13整除的最大的整数,并输出。
代码:
for i in range(200,0,-1):
if i%13==0:
break
print'the biggest one that can be divided by 13 in 200 is:',i
结果:
the biggest one that can be divided by 13 in 200 is: 195
1.输入三角形的三条边,判断是否能组成三角形。若是可以,计算三角形的面积。
代码:
import math
a,b,c=input('please input 3 numbers(with ","in them):')
if a+b>c and a+c>b and b+c>a:
print 'this is a triangle !'
p=(a+b+c)/2
areaspr=p*(p-a)*(p-b)*(p-c)
area=math.sqrt(areaspr)
print 'area=',area
else:
print'that is not triangle!'
结果:
please input 3 numbers(with ","in them):3,4,5
this is a triangle !
area= 6.0
please input 3 numbers(with ","in them):1,2,6
that is not triangle!
2.循环 主要使用while和 for实现
1、询问用户输入一个成绩,计算平均成绩输出,并询问是否继续。
代码:
sumall=0.0
num=0
endflag='y'
while endflag=='y':
score=input('please input a score!')
num=num+1
sumall=sumall+score
mean=sumall/num
print'the mean is:',mean
endflag=raw_input('would you like to go on?(y/n)')
结果:
please input a score!89
the mean is: 89.0
would you like to go on?(y/n)y
please input a score!87
the mean is: 88.0
would you like to go on?(y/n)y
please input a score!68
the mean is: 81.3333333333
would you like to go on?(y/n)n
2、输入一个列表,输出里面所有偶数元素的和
代码:
num_list=input('please input a list:')
sum=0
for num in num_list:
if num%2==0:
sum=sum+num
print 'the sum of even number is:',sum
结果:
please input a list:[0,4,3,5,7,8]
the sum of even number is: 12
3、while解决不确定次数的循环:从键盘输入若干整数,求所有输入的正数的和,遇到负数以后便结束该操作。
代码:
sum=0
num=input('please input a number,a negtive means the end:')
while num>0:
sum=sum+num
num=input('please input a number,a negtive means the end:')
print'the sum of positive is :',sum
print'the end!'
结果:
please input a number,a negtive means the end:4
please input a number,a negtive means the end:5
please input a number,a negtive means the end:7
please input a number,a negtive means the end:2
please input a number,a negtive means the end:-1
the sum of positive is : 18
the end!
4、计算s=1+.....+1/n!
代码:
num=input('please input a number:')
sum=0.0
sumall=0.0
n=1
while n<=num:
sum=sum+n
sumall=sumall+1/sum
n=n+1
print 'the addition is:',sumall
结果:
please input a number:11
the addition is: 1.83333333333
5、求出0-100中能被7整除,但是不能被5整除的所有整数。
for i in range(101):
if i%7==0 and i%5 !=0:
print i
结果:
7
14
21
28
42
49
56
63
77
84
91
98
6、使用FOR循环输出9*9乘法表:
代码:
for i in range(1,10):
for j in range(i,10,1):
print i,'*',j,'=',i*j,'\t',
print '\n'
结果:
1 * 1 = 1 1 * 2 = 2
1 * 3 = 3 1 * 4 = 4
1 * 5 = 5 1 * 6 = 6
1 * 7 = 7 1 * 8 = 8
1 * 9 = 9
2 * 2 = 4 2 * 3 = 6
2 * 4 = 8 2 * 5 = 10
2 * 6 = 12 2 * 7 = 14
2 * 8 = 16 2 * 9 = 18
3 * 3 = 9 3 * 4 = 12
3 * 5 = 15 3 * 6 = 18
3 * 7 = 21 3 * 8 = 24
3 * 9 = 27
4 * 4 = 16 4 * 5 = 20
4 * 6 = 24 4 * 7 = 28
4 * 8 = 32 4 * 9 = 36
5 * 5 = 25 5 * 6 = 30
5 * 7 = 35 5 * 8 = 40
5 * 9 = 45
6 * 6 = 36 6 * 7 = 42
6 * 8 = 48 6 * 9 = 54
7 * 7 = 49 7 * 8 = 56
7 * 9 = 63
8 * 8 = 64 8 * 9 = 72
9 * 9 = 81
上面的print i,'*',j,'=',i*j,'\t', 中最后一个,很重要
7、求200以内能够被13整除的最大的整数,并输出。
代码:
for i in range(200,0,-1):
if i%13==0:
break
print'the biggest one that can be divided by 13 in 200 is:',i
结果:
the biggest one that can be divided by 13 in 200 is: 195
相关文章推荐
- python基础 循环结构程序设计(上)
- python基础 循环结构程序设计
- Python基础(2)——Python选择、循环结构
- python基础 循环结构程序设计(下)
- Python新手学习基础之循环结构练习
- C++第4次实验(基础班)—循环结构程序设计
- Linux C程序设计基本结构——顺序、选择、循环
- Python学习入门基础教程(learning Python)--4 Python的循环结构
- Java 基础 选择结构程序设计
- 顺序结构、选择结构和循环结构的程序设计例题
- 黑马程序员_java基础学习笔记02_选择结构和循环语句语句
- Python基础05 三种基本结构&选择结构
- 我与python约个会:12程序编程基础6:选择和循环
- Linux C程序设计基本结构——顺序,选择与循环
- python if的选择结构与循环
- C++第3次实验(基础班)—选择结构程序设计
- Python新手学习基础之循环结构——循环控制break continue pass
- 选择结构的程序设计和循环结构的程序设计
- C++第4次作业(基础班)—循环结构程序设计
- JAVA选择结构语句和witchi和for循环判断语句jj基础介绍