chap4 条件

chap4 条件

1.if语句

if ():



···
<following_statement>
如果输入正确则会执行程序,否则不会执行if语句里的内容

if else语句

x=input("x=") x=float(x) print("hello") if x<10: print("wahoo") else: print("oh no") print("goodbye")
if else有选择性 可以有嵌套

if-elif-else语句

print("hello") if x < 10: print("wahoo") elif x <=99: print("meh") else: print("hahahaha") print("goodbye")
if x<10为真会输出wahoo 如果x>10且<=99输出meh 否则输出hahahaha

match-case语句

def f(x): match x: case 400: return "A" case 404: return "B" case 418: return "C" case_: return "not found"
类似于switch-case语句

posted @ 2023-11-28 19:44  xiruan  阅读(41)  评论(0)    收藏  举报