Python超入门(3)__迅速上手操作掌握Python

# 11.if语句
is_student = True  # bool类型
is_teacher = False

if is_student:
    print("请到操场集合")
elif is_teacher:
    print("请到办公室集合")
else:
    print("请离开学校")
print("谢谢合作")
"""
请到操场集合
谢谢合作
"""
练习:有一栋价值一百万美元的房子,,当你遇上一个好买家时,可以获得20%的利润;否则为10%;
输出获得的利润。
price = 1000000
is_good_buyer = True
if is_good_buyer:
    gain_profit = price * 0.2
else:
    gain_profit = price * 0.1

print(f'You will getting ${gain_profit}')
# 12.逻辑运算符(and, or, not)优先级:not > and > or
has_high_income = True
has_good_credit = False
has_criminal_record = False
if (has_high_income or has_good_credit) and not has_criminal_record:  # (T or F) and T
    print("允许贷款")
else:
    print("不允许贷款")
"""
允许贷款
"""
# 13. 比较运算符(>; >=; <; <=; !=; ==)
temperature = int(input("请输入今天的气温:"))
if temperature >= 30:
    print("今天是炎热的一天")
elif 30 > temperature > 20:
    print("今天是温暖的一天")
else:
    print("今天要加衣服了")
"""
请输入今天的气温:25
今天是温暖的一天
"""
练习:你的名字长度必须不少于2个字符,且不能超过20个字符;
符合规定的是好名字。
name = input("请输入你的名字: ")
if len(name) < 2:
    print("你的名字长度必须大于2个字符")
elif len(name) > 20:
    print("你的名字长度必须小于20个字符")
else:
    print("这是一个好名字")
# 14.利用所学知识建立一个重量转换器小程序
weight = int(input("weight: "))
unit = input("(L)bs or (K)g: ")
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("Print error!")
"""
weight: 120
(L)bs or (K)g: l
You are 54.0 kilos
"""

猜你喜欢

转载自blog.csdn.net/qq_57233919/article/details/132783723