"""第 3 天学生练习：变量、类型、比较与逻辑。

要求：
1. 只修改 TODO 指定的位置。
2. 每个实验先在注释中写出预测，再运行。
3. 报错实验完成后要恢复正确代码。
"""

from decimal import Decimal
from math import isclose


print("=" * 64)
print("第一部分：建立商品数据")

# TODO 1：填写一种商品名称，类型应为 str。
product_name = "请填写商品名称"

# TODO 2：填写销售数量，类型应为 int，建议使用 125。
sales_quantity = 0

# TODO 3：填写退单数量，类型应为 int，建议使用 8。
return_quantity = 0

# TODO 4：填写商城价格，类型应为 float，建议使用 199.0。
unit_price = 0.0

# TODO 5：当前没有优惠码，用 None 表示。
coupon_code = "请修改"

# TODO 6：使用 sales_quantity >= 100 得到 bool。
quantity_reached_target = False

print("商品名称：", product_name)
print("销售数量：", sales_quantity)
print("退单数量：", return_quantity)
print("商城价格：", unit_price)
print("优惠码：", coupon_code)
print("数量是否达标：", quantity_reached_target)


print("\n第二部分：检查值与类型")

# TODO 7：把下面占位文字改成对应的 type(...)。
print("product_name 类型：", "请修改")
print("sales_quantity 类型：", "请修改")
print("unit_price 类型：", "请修改")
print("quantity_reached_target 类型：", "请修改")
print("coupon_code 类型：", "请修改")

# TODO 8：使用 isinstance() 判断 sales_quantity 是否为 int。
print("销售数量是否为整数：", "请修改")

# TODO 9：使用 isinstance(value, (int, float)) 判断价格是否为数值。
print("价格是否为数值：", "请修改")


print("\n第三部分：数字与数字文本")

quantity_number = 125
quantity_text = "125"

print("数值：", repr(quantity_number), type(quantity_number))
print("文本：", repr(quantity_text), type(quantity_text))

# 运行前预测：
# "12" + "3" 的结果：
# 12 + 3 的结果：
print('"12" + "3" =', "12" + "3")
print("12 + 3 =", 12 + 3)

# TODO 10：先把 quantity_text 转为 int，再与 quantity_number 相加。
converted_total = 0
print("转换后相加：", converted_total)


print("\n第四部分：浮点数精度")

float_result = 0.1 + 0.2
decimal_result = Decimal("0.1") + Decimal("0.2")

print("0.1 + 0.2 =", float_result)
print("是否严格等于 0.3：", float_result == 0.3)
print("是否足够接近 0.3：", isclose(float_result, 0.3))
print("Decimal 结果：", decimal_result)

# TODO 11：使用 round() 把 float_result 保留两位小数。
rounded_result = float_result
print("舍入结果：", rounded_result)


print("\n第五部分：边界值")

quantity_99 = 99
quantity_100 = 100
quantity_101 = 101

# 先预测 > 100 的三个结果：
# 99：
# 100：
# 101：
print("99 > 100：", quantity_99 > 100)
print("100 > 100：", quantity_100 > 100)
print("101 > 100：", quantity_101 > 100)

# TODO 12：再分别使用 >= 100，观察边界 100 是否被选中。
print("99 >= 100：", "请修改")
print("100 >= 100：", "请修改")
print("101 >= 100：", "请修改")


print("\n第六部分：多条件逻辑")

condition_quantity = sales_quantity >= 100
condition_price = unit_price < 200
condition_returns = return_quantity <= 10

# TODO 13：使用 and 组合三个条件。
is_target_product = False

print("数量条件：", condition_quantity)
print("价格条件：", condition_price)
print("退单条件：", condition_returns)
print("是否为目标商品：", is_target_product)

# TODO 14：创建“数量达标或者价格低于 50”的条件，使用 or。
is_special_case = False
print("是否为特殊情况：", is_special_case)

# TODO 15：使用 not 表示“数量没有达标”。
quantity_not_reached = False
print("数量没有达标：", quantity_not_reached)


print("\n第七部分：None 与真值")

print("coupon_code is None：", coupon_code is None)
print('bool("")：', bool(""))
print('bool("False")：', bool("False"))
print("bool(0)：", bool(0))
print("bool(None)：", bool(None))

# 写出解释：
# 为什么 bool("False") 是 True：


print("\n第八部分：业务计算预备")

# TODO 16：先算净销售数量，再计算净销售额。
net_quantity = 0
net_sales = 0.0

print("净销售数量：", net_quantity)
print("净销售额：", net_sales)


# 错误实验一：
# 取消下一行注释，观察 str 与 int 相加产生的 TypeError，然后重新注释。
# print("销量：" + sales_quantity)

# 错误实验二：
# 取消下一行注释，观察文字未加引号产生的 NameError，然后重新注释。
# wrong_product = 双肩包

# 错误实验三：
# 暂时执行 type = "VIP"，再调用 type(125)，观察覆盖内置名称的后果。
# 完成后删除该变量或重启运行环境。

# 最终自检：
# [ ] 能解释 = 与 == 的区别。
# [ ] 能解释 125 与 "125" 的区别。
# [ ] 能解释 > 100 与 >= 100 的边界差异。
# [ ] 能解释 True 与 "True" 的区别。
# [ ] 能说明 None 不等于 0、空字符串和 False。
