第2章 Python 分支结构
文章目錄
- Educoder—第2章 Python 分支結構
- 第1關:Python單路分支之求平拋小球與拋出點之間的距離
- 第2關:Python單路分支之正方形判斷
- 第3關:Python雙路分支之溫度轉換
- 第4關:Python雙路分支之閏年的判斷
- 第5關:Python多路分支之天數判斷
- 第6關:Python多路分支之一元二次方程求解
Educoder—第2章 Python 分支結構
本章目標是通過學習具備編寫 Python 分支結構程序的能力,涉及的 Python 編程基本概念包括:布爾數據類型( boolean ),關系運算符、邏輯運算符及表達式,分支語句 if、if-lese、if-elif-else 等。
第1關:Python單路分支之求平拋小球與拋出點之間的距離
任務:一小球以 5米/秒 的水平速度平拋,重力加速度取9.8米每秒的平方,在忽略空氣阻力的情況下,求經過時間 t 秒后(t 是獲取的輸入值),小球所在位置與拋出點之間的距離 (假設小球距地面足夠高,t應大于0)。
如果t>0,輸出格式為:"經過t秒后,小球與原點的距離為d米"
如果t<0,輸出格式為:"t<0,不合法"
from math import* G = 9.8 # 聲明浮點型變量 G,用于表示重力加速度 v0 = 5 # 聲明整型變量 v0, 用于表示水平初速度t = int(input()) s = v0 * t h = 0.5 * G * pow(t,2) d = sqrt((pow(s,2) + pow(h,2))) if t > 0:print("經過%.6f秒后,小球與原點的距離為%.6f米"%(t,d)) else :print("t<0,不合法")執行結果; D:\網絡安全\Python\py_code>python educ.py please input time:1 經過1.000000秒后,小球與原點的距離為7.000714米D:\網絡安全\Python\py_code>python educ.py please input time:5 經過5.000000秒后,小球與原點的距離為125.024998米第2關:Python單路分支之正方形判斷
任務:假設現在有一個方形,它的長度和寬度未知,只知道長和寬的變量名,請編寫代碼判斷該方形是否為正方形(長和寬都應大于 0)。
輸出格式:
如果長度小于等于0輸出"長度不合法",
如果寬度小于等于0,則輸出"寬度不合法",
如果長度等于寬度,則輸出"該方形為正方形",
如果長度不等于寬度,則輸出"該方形為長方形"。
leng = int(input("please input length:")) wid = int(input("please input width:")) if leng<=0:print("長度不合法") elif wid<=0:print("寬度不合法") elif leng==wid:print("該方形為正方形") elif leng!=wid:print("該方形為長方形")執行結果: D:\網絡安全\Python\py_code>python educ.py please input length:1 please input width:2 該方形為長方形D:\網絡安全\Python\py_code>python educ.py please input length:1 please input width:1 該方形為正方形D:\網絡安全\Python\py_code>python educ.py please input length:-1 please input width:-2 長度不合法第3關:Python雙路分支之溫度轉換
任務:根據輸入的選項,完成從攝氏度到華氏度或從華氏度到攝氏度的轉換。
輸入數據包括溫度的單位、待轉換的溫度值,溫度值為浮點型。攝氏度的單位可能為攝氏度,也可能為 C,華氏度的單位可能為華氏度,也可能為 F
輸出格式為:"c攝氏度轉換為f華氏度"
while True:unit = str(input("please input the unit:"))value = float(input("please input the value:"))if unit == "C" or unit == "攝氏度":f = value*1.8+32print("{:.6f}攝氏度轉換為{:.6f}華氏度".format(value,f))elif unit == "F" or unit == "華氏度":c = (value-32)/1.8print("{:.6f}華氏度轉換為{:.6f}攝氏度".format(value,c))執行結果; D:\網絡安全\Python\py_code>python educ.py please input the unit:攝氏度 please input the value:10 10.000000攝氏度轉換為50.000000華氏度 please input the unit:C please input the value:15 15.000000攝氏度轉換為59.000000華氏度 please input the unit:F please input the value:39.8 39.800000華氏度轉換為4.333333攝氏度 please input the unit:華氏度 please input the value:32 32.000000華氏度轉換為0.000000攝氏度 please input the unit:C please input the value:-1 -1.000000攝氏度轉換為30.200000華氏度第4關:Python雙路分支之閏年的判斷
任務:假設現在我們已知年份為 year,請編寫代碼判斷這一年是否為閏年。
輸出格式:"year年是閏年"或者"year年是平年"
分析:判斷一個年份是否為閏年主要取決于兩點,年份能被 400 整除,或者能被 4 整除但不能被 100 整除的就是閏年。假設現在我們已知年份為 year,請編寫代碼判斷這一年是否為閏年。
while True:year = int(input("please input the year:"))if year%400 == 0 or (year%4==0 and year%100!=0):print("{}年是閏年".format(year))else:print("{}年是平年".format(year))執行結果: D:\網絡安全\Python\py_code>python educ.py please input the year:2020 2020年是閏年 please input the year:1984 1984年是閏年 please input the year:2017 2017年是平年 please input the year:1500 1500年是平年第5關:Python多路分支之天數判斷
任務:根據輸入的年份和月份判斷該月的天數。
一年中,1、3、5、7、8、10、12月有31天,4、6、9、11月有30天,
閏年的2月有29天,平年的2月有28天。
輸出格式:"year年month月有30天"
做法1:根據閏平年來做區分 year = int(input("please input the year:")) month = int(input("please input the year:")) if year%400 == 0 or (year%4==0 and year%100!=0):if month == 2:print("{}年{}月有29天".format(year,month))elif month == 4 or month == 6 or month == 9 or month == 11print("{}年{}月有30天".format(year,month))else:print("{}年{}月有31天".format(year,month)) else:if month == 2:print("{}年{}月有28天".format(year,month))elif month == 4 or month == 6 or month == 9 or month == 11print("{}年{}月有30天".format(year,month))else:print("{}年{}月有31天".format(year,month))做法2:根據2月這個特殊月份來做區分,建議用做法2這種,即找到大的區分 while True:year = int(input("please input the year:"))month = int(input("please input the year:"))if month == 4 or month == 6 or month == 9 or month == 11:print("{}年{}月有30天".format(year,month))elif month == 2:if year%400 == 0 or (year%4==0 and year%100!=0):print("{}年{}月有29天".format(year,month))else:print("{}年{}月有28天".format(year,month))else:print("{}年{}月有31天".format(year,month))執行結果: D:\網絡安全\Python\py_code>python educ.py please input the year:2020 please input the year:2 2020年2月有29天 please input the year:2019 please input the year:7 2019年7月有31天 please input the year:1965 please input the year:2 1965年2月有28天第6關:Python多路分支之一元二次方程求解
任務:求解一元二次方程 ax2+bx+c=0 的根,系數 a、b、c 的值從輸入獲取。(本關a,b,c都是整型)
please input b:1
please input c:3
-3.0
please input a:2
please input b:4
please input c:2
-1.0
please input a:3
please input b:3
please input c:1
無解
please input a:4
please input b:5
please input c:1
x1為-0.250000,x2為-1.000000
總結
以上是生活随笔為你收集整理的第2章 Python 分支结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51单片机的频率计设计
- 下一篇: 计算机叶老师,叶茫 - 教师简历 CV-