def computeHCF(x,y): if (x > y): smaller = y else: smaller = x for i in range(1, smaller+1): if((x % i == 0) and (y % i == 0)): hcf = i print('公因數=',hcf) return hcf print('計算兩整數的公因數, 請輸入兩個整數:') num1=int(input("整數1=")) num2=int(input("整數2=")) HCF=computeHCF(num1, num2) print(num1, num2, '的最大公因數=',HCF) |
==========================螢幕上的輸出============ 計算兩整數的公因數, 請輸入兩個整數: 整數1=12 整數2=18 公因數= 1 公因數= 2 公因數= 3 公因數= 6 12 18 的最大公因數= 6 |
def rt(a): for n in range(1,a+1): print('*'*n) while True: b=input("直角三角形的邊長=(輸入0就終止)") if(b == 0): break c=int(b) rt(c) |
==========================螢幕上的輸出============ 直角三角形的邊長=(輸入0就終止)3 * ** *** 直角三角形的邊長=(輸入0就終止)6 * ** *** **** ***** ****** 直角三角形的邊長=(輸入0就終止)0 直角三角形的邊長=(輸入0就終止) |
def fact(n): # n是由主程式傳入之參數 m=1 for n in range(1,n+1): m=m*n return m # 回傳m至主程式的呼叫點 m,n=10,3 Cmn=fact(m)/(fact(n)*fact(m-n)) print ('C(%2d,%2d)=%4d' %(m,n,Cmn)) |
==========================螢幕上的輸出============ C(10, 3)= 120 ================================================= |
import math def root2(a,b,c): D=b**2-4.*a*c if(D == 0.): x1,x2=-b/2./a, -b/2./a elif D > 0.: x1=(-b+math.sqrt(D))/2./a x2=(-b-math.sqrt(D))/2./a else: import cmath #有複數的數學函數 x1=(-b+cmath.sqrt(D))/2./a #複數開平方 x2=(-b-cmath.sqrt(D))/2./a return (D,x1,x2) #回傳元組 a,b,c=1.,-2.,-3. (D,x1,x2)=root2(1.,-2.,-3.) #以元組方式傳回 print('a,b,c=',a,b,c) if(D > 0): print('兩實根=',(x1,x2)) #列印元組 else: print('D < 0, D=',D) print('兩複數根=',x1,x2) print(' ') a,b,c=1.,-2.,3. (D,x1,x2)=root2(1.,-2.,3.) #以元組方式傳回 print('a,b,c=',a,b,c) if(D > 0): print('兩實根=',(x1,x2)) #列印元組 else: print('D < 0, D=',D) print('兩複數根=',x1,x2) |
==========================螢幕上的輸出============ a,b,c= 1.0 -2.0 -3.0 兩實根= (3.0, -1.0) a,b,c= 1.0 -2.0 3.0 D < 0, D= -8.0 兩複數根= (1+1.4142135623730951j) (1-1.4142135623730951j) ================================================= |
Web VPython 3.2 # filepath: https://physexp.thu.edu.tw/~AP/YC/NUM/HTML/mag-bottle-tr.txt f = read_local_file(scene.title_anchor) print(f.name) # The file name print(f.size) # File size in bytes print(f.type) # What kind of file print(f.date) # Creation date if available print('-------------------') #print(f.text) # The file contents f=f.text a=f print(type(a)) a=a.strip() a2=a.split(' ') b=list(a2) #print(b) print('len(b)=',len(b),type(b),type(b[0]),b[0]) for i in range(len(b)): b[i]=float(b[i]) def traject(b): ball = sphere(radius = 0.1, color=vec(1,1,0),make_trail=True, retain=100) Nb=len(b) #print('Nb=',Nb) N=Nb/4 t=[float(b[i*4+0]) for i in range(N)] x=[float(b[i*4+1]) for i in range(N)] y=[float(b[i*4+2]) for i in range(N)] z=[float(b[i*4+3]) for i in range(N)] LX=max(x) LY=max(y) LZ=max(z) sw=LX/100. arrX=arrow(pos=vec(0,0,0),axis=vec(LX,0,0),shaftwidth=sw,color=vec(1,0,0)) arrY=arrow(pos=vec(0,0,0),axis=vec(0,LY,0),shaftwidth=sw,color=vec(0,1,0)) arrZ=arrow(pos=vec(0,0,0),axis=vec(0,0,LZ),shaftwidth=sw,color=vec(0,0,1)) ball.pos=vec(x[0],y[0],z[0]) for i in range(N): rate(10) ball.pos=vec(x[i],y[i],z[i]) #print(t[i],x[i],y[i],z[i]) scene = canvas(width=800, height=800, center=vec(0,0,0),background=vec(0,0,0)) print('====',type(b)) traject(b) |
====================螢幕上的輸出============ mag-bottle-tr[1].txt 3.3642e+4 text/plain 2023/3/2 ------------------- |