>、=、<= 等等均为比较运算符
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
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")