Discuss / Python / 该更新了,3.10更新了match语法

该更新了,3.10更新了match语法

Topic source

C

#1 Created at ... [Delete] [Delete and Lock User]
h = input('height')
w = input('weight')

bmi = w / h**2

match bmi:
    case bmi <= 18.5:
        print('过轻')
    case 18.5 < bmi <= 25:
        print('正常')
    case 25 < bmi <= 28:
        print('过重')
    case 28 < bmi <= 32:
        print('肥胖')
    case _:
        print('严重肥胖')

Risin

#2 Created at ... [Delete] [Delete and Lock User]

1 没有转数据类型

2 直接在case里用比较运算会报错

heigh = float(input("身高(m): "))

weigh = float(input("体重(kg): "))

bmi = weigh / (heigh**2)

match bmi:

    case _ if bmi <= 18.5:

        print("过轻")

    case _ if 18.85 <= bmi <= 25:

        print("正常")

    case _ if 25 <= bmi <= 28:

        print("超重")

    case _:

        print("肥胖")


  • 1

Reply