1. 程式人生 > >python程式設計基礎3:python選擇和迴圈結構

python程式設計基礎3:python選擇和迴圈結構

這一節的知識點很簡單:選擇: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