(1)單分支結構:
guess =
eval
(input()
)if guess ==99:
print
("猜對了!"
)
或者
if
true
:print
("猜對了!"
)
(2)二分支結構:
guess =
eval
(input()
)if guess ==99:
print
("猜對了!"
)else
:print
("猜錯了!"
)
或者
if
true
:print
("猜對了!"
)else
:print
("猜錯了!"
)
或者
#二分支結構的 緊湊形式
guess =
eval
(input()
)print
("猜{}了"
.format
("對"
if guess ==
99else
"錯")
)#如果 guess 等於 99,則返回 "對" ,輸出 "猜對了";相反輸出 "猜錯了"
(3)多分支結構:
score =
eval
(input()
)if score >=90:
grade =
"a"elif score >=80:
grade =
"b"elif score >=70:
grade =
"c"elif score >=60:
grade =
"d"else
: grade =
"f"print
("輸入的成績屬於級別{}"
.format
(grade)
)
(4)條件判斷:
score =
eval
(input()
)if score >
10or score <20:
# or 可以改為 and
print
("猜錯了"
)else
:print
("猜對了"
)
if
nottrue
:#這樣的**,永遠不會執行語句塊2
print
("語句塊2"
)else
:print
("語句塊1"
)
# not 的用法
a=false
ifnot a:
print
("hello world"
)
# not 的用法
a =5
b =[1,
2,3]
if a not
in b:
print
("hello world!"
)
(5)程式的異常處理:![](https://pic.w3help.cc/b95/c037ca46c89f73f888be96222ce15.jpeg)
用法:我們把乙個語句塊1,放到 try 中,如果它有問題(異常),則語句塊1不會被執行,進而跳到 except 執行語句塊2。
也可以在 except 中加入 異常型別 ,進而只有發生這種異常型別時才執行語句塊2。
("請輸入身高(公尺)和體重\(公斤)[逗號隔開]:"))
bmi=weight/
pow(height,2)
who,nat="",
""if bmi<
18.5
: who,nat=
"偏瘦"
,"偏瘦"
elif
18.5
<=bmi<24:
who,nat=
"正常"
,"正常"
elif
24<=bmi<25:
who,nat=
"正常"
,"偏胖"
elif
25<=bmi<28:
who,nat=
"偏胖"
,"偏胖"
elif
28<=bim<30:
who,nat=
"偏胖"
,"肥胖"
else
: who,nat=
"偏胖"
,"偏胖"
("bmi指標為:國際'',國內''"
.format
(who,nat)
)'''
注意事項:
height,weight=eval("2,9")
print("{} {}".format(height,weight))
>>>2 9
看了之後你就大概明白是啥意思了吧?
注:input()函式將所有輸入作為字串看待。
'''
小程式(第四周)
include include void main int max 20 2 scanf d a 資料組數 if a 0 for i 0 i0 j max i 0 num i 0 while num i z max i 0 max i 1 num i z z 0 for i 0 i這道題的輸入輸出樣...
第四周 Python反射
反射 reflection 指的是在執行時獲取型別的定義資訊。本質 利用字串的形式去物件 模組 中操作 查詢 獲取 刪除 新增 成員,是一種基於字串的事件驅動。簡單來說,python可以通過字串來操作物件 類 方法 模組的 屬性和方法 也可操作類 這就是python的反射。在python中例項化物件...
第四周作業
第四周實驗作業 1.完成課本每乙個程式設計題。要求先畫出流程演算法圖或n s圖,然後程式設計實現,有可能的話使用兩種以上方法 1.1求自然對數e的近似值 include using namespace std int main cout 1.2求圓周率 的近似值 include using name...