Python基础(十三)---比较运算符

Jenny ·
更新时间:2024-11-15
· 669 次阅读

比较运算符:

 >、=、<=  等等均为比较运算符

e.g.1

    if temperature is greater than 30
        it’s a hot day
    otherwise if it’s less than 10
        it’s a cold day
    otherwise
        its neither hot nor cold

Solution: temperature = 25 if temperature > 30: print("it's a hot day") elif temperature < 10: print("it's a cold day") else: print("its neither hot nor cold") e.g.2

  输入某人的体重weight,判断其类型(pounds/kg),并转换为另一种体重类型:

Solution: weight = int(input(" weight: ")) unit = input(' (L)bs or (K)g: ') # upper()方法将小写字母转换为大写字母 if unit.upper() == 'L': converted = weight*0.45 print(f"You are {converted} kilos ") elif unit.upper() == 'k': converted = weight/0.45 print(f"You are {converted} pounds ") else: print("Not true, please do it again")
作者:你隔壁八楼の王



比较运算符 Python python基础 运算符

需要 登录 后方可回复, 如果你还没有账号请 注册新账号