資源簡介 2.3程序設計基本知識P60實踐活動編程計算體重指數程序設計答案:a=float(input('請輸入身高米:'))b=float(input('請輸入體重千克:'))BMI=b/(a*a)print("體重指數BMI=",round(BMI,2)) #保留1位小數P63實踐活動評定體重指數等級程序設計答案:第2題:方案一b=float(input('請輸入身高米:'))c=float(input('請輸入體重千克:'))bmi=c/pow(b,2)t=bmiprint("你的體重指數BMI的值是:",round(t,1))if t>=16.5 and t<=23.3:print("正常")else :if t<=16.4:print('低體重')else:if t>=23.3 and t<=26.3:print("超重")else:print("肥胖")方案二:多分支結構b=float(input('請輸入身高米:'))c=float(input('請輸入體重千克:'))bmi=c/(b*b)t=bmiprint("體重指數BMI的值是:",round(t,1))if t>=16.5 and t<=23.3:print("正常")elif t<=16.4:print('低體重')elif t>=23.3 and t<=26.3:print("超重")else:print("肥胖")第3題:a=input('請輸入性別:')e='男'f="女"if a!=e and a!=f:print("性別輸入錯誤")else:b=float(input('請輸入身高米:'))c=float(input('請輸入體重千克:'))bmi=c/pow(b,2)t=bmiif a==e:if t>=16.5 and t<=23.3:print("正常")elif t<=16.4:print('低體重')elif t>=23.3 and t<=26.3:print("超重")else:print("肥胖")else:if a==f:if t>=16.5 and t<=22.7:print("正常")else:if t<=16.4:print('低體重')else:if t>=22.8 and t<=25.2:print("超重")else:print("肥胖")P67實踐活動對多名學生進行體重指數的等級判斷的程序設計。i=1rs=int(input('請輸入學生人數:'))while i<=rs:i=i+1a=input('請輸入性別:')e='男'f="女"if a!=e and a!=f:print("性別輸入錯誤")else:b=float(input('請輸入身高米:'))c=float(input('請輸入體重千克:'))bmi=c/pow(b,2)t=bmiprint("體重指數BMI的值是:",round(t,1))if a==e:if t>=16.5 and t<=23.3:print("正常")elif t<=16.4:print('低體重')elif t>=23.3 and t<=26.3:print("超重")else:print("肥胖")else:if a==f:if t>=16.5 and t<=22.7:print("正常")else:if t<=16.4:print('低體重')else:if t>=22.8 and t<=25.2:print("超重")else:print("肥胖")P69練習提升1、分別指出下面程序中循環體執行的次數,寫出運行結果,分析程序功能。(1)i=100r=1 #執行次數while i>1:print("進入第",r,"次循環,r=",r)r=r+1if i%13==0:breaki=i-1print(i)答案:程序執行的次數為10次,輸出i的值為91。(2)r=1 #執行次數for i in range(100,1,-1):print("進入第",r,"次循環,r=",r)r=r+1if i%13!=0:continueprint(i)答案:程序執行的次數為99,出i的值為91、78、65、52、39、26、13。2.設計算法編寫程序實現如下功能:已知一元二次方程ax2 +bx+c=0,從鍵盤輸入a、b和c的值,獲取該一元二次方程解的情況。import matha = float(input("請輸入a的值:"))b = float(input("請輸入b的值:"))c = float(input("請輸入c的值:"))if a != 0:delta = b ** 2 - 4 * a * cif delta < 0:print("方程無解")elif delta == 0:s = -b / (2 * a)print("兩根相等:x=",s)else :root = sqrt(delta)x1 = (-b + root) / (2 * a)x2 = (-b - root) / (2 * a)print("x1=", x1, "\t", "x2=", x2)3.求任意兩個正整數的最大公約數,直至用戶輸入”q”或”Q”才退出程序。while True:a = int(input("輸入第一個正整數:"))b = int(input("輸入第一個正整數:"))while b:x = a % ba = bb = xprint("兩個數的最大公約數是:",a)c = input("是否繼續?(退出程序請輸入‘q’或‘Q’;按其它鍵則繼續)")if c == "q" or c == "Q":break4.編寫程序和計算機來一場有趣的猜數游戲。要求:由計算機隨機生成一個100以內的正整數,用戶有五次猜數機會,如果猜中提示”猜中了!“并退出程序;否則,提示”數偏大“或”數偏小“,然后繼續猜數。如果5次均未猜中,屏幕顯示計算機隨機生成的整數,并換行給出提示“五次沒有猜中,很遺憾,游戲結束“,退出程序。import randomnumber = 5secret = random.randint(1, 100)while number > 0:temp1 = int(input("輸入一個整數(1-100):"))if temp1 == secret:print("猜中了!")breakelif temp1 > secret:print("數偏大,還剩", number - 1, "次機會")else:print("數偏小,還剩", number - 1, "次機會")number -= 1if number == 0:print("答案是", secret)print("5次沒有猜中,很遺憾,游戲結束") 展開更多...... 收起↑ 資源預覽 縮略圖、資源來源于二一教育資源庫