1.輸入平面上兩個點,計算兩點的距離
import math
x1,y1=input('please the start point x1,y1:')
x2,y2=input('please the start point x2,y2:')
distance=math.sqrt((x1-x2)**2+(y1-y2)**2)
print'distance=',distance
please the start point x1,y1:0,0
please the start point x2,y2:3,4
distance= 5.0
2.任意輸入3個單詞,將他們按字典順序排列
string=raw_input('please input 3words with "," in them:')
x,y,z=string.split(',')
if x>y:
x,y=y,x
if x>z:
x,z=z,x
if y>z:
y,z=z,y
print x,y,z
please input 3words with "," in them:iker,peng,xiao
iker peng xiao
3. 解二元一次方程組。輸入他們的係數,輸出結果。(我們使用了numpy 的庫)
import numpy as np
a=np.zeros((2,3))
a[0][0],a[0][1],a[0][2]=input('please input 3 numbers for the first function:')
a[1][0],a[1][1],a[1][2]=input('please input 3 numbers for the second function:')
if a[0][0]*a[1][1]==0:
print "are you kidding me?"
else:
a[1]=a[0]*(-a[1][0]/a[0][0])+a[1]
a[0]=a[1]*(-a[0][1]/a[1][1])+a[0]
print 'the answer is:x1=',a[0][2]/a[0][0],'x2=',a[1][2]/a[1][1]
please input 3 numbers for the first function:1,2,3
please input 3 numbers for the second function:4,9,7
the answer is:x1= 13.0 x2= -5.0
please input 3 numbers for the first function:0,1,2
please input 3 numbers for the second function:1,2,3
are you kidding me?
4,矩陣按其形狀輸出
a=input('please input a 3*3 array:')
for x in a:
s=''
for y in x:
s1='%6d'%y
s=s+s1
print s
please input a 3*3 array:[[1,2,1],[2,3,4],[4,5,0]]
1 2 1
2 3 4
4 5 0