# bool输入 将空字符串作为False
is_hot = bool(input("t or f"))
运算符
# 逻辑运算符
# && and
# || or
# ! not 但是要表达多个之间,需要用上面的两个词作为连接词
has_high_income = False
has_good_credit = True
if has_good_credit or has_high_income:
print("get a little loan ")
if has_good_credit and has_high_income:
print("get loan")
else:
print("not get loan")
if has_good_credit and not has_high_income:
print("say no")
if not has_high_income:
print("so poverty")
# 比较运算符 >= <= > < == !=
temperature = 30
if temperature == 30:
print("It's hot day")
else:
print("It's not hot day")
test 1 ,体重转换器
- # 输入kg转换为bs bs转换kg
- user_weight = input("Weight : ")
- option = input("(L)bs or (K)g: ")
- # if option == 'L' or option == 'l':
- if option.upper()=='L':
- print(f"you are {float(user_weight) * 0.45} kg")
- elif option.upper() == 'K':
- print(f"you are {float(user_weight) / 0.45} pounds")
- else:
- print("your option had error! ")
- # index的正负应用
- name = "Jennifer"
- print(name[1:-1])
test2:简单比较运算符应用
- name = input("What's your name? ")
- length = len(name)
- if length < 3:
- print("the name must be at least 3 characters")
- elif length > 50:
- print("the name can be a maximum of 50 characters")
- else:
- print(f'name: {name} looks good!')