"""2.1-2.4 课后练习参考答案：代码格式、变量、输入与输出。"""

print("=" * 64)
print("第一部分：变量名体检")

sales_quantity_legal = True
two_price_legal = False
class_legal = False
unit_price_legal = True

print("sales_quantity 合法：", sales_quantity_legal)
print("2price 合法：", two_price_legal)
print("class 合法：", class_legal)
print("unit_price 合法：", unit_price_legal)


print("\n第二部分：建立一条商品记录")

product_name = "数据分析入门"
sales_quantity = 3
unit_price = 19.9

print("商品：", product_name)
print("数量：", sales_quantity, type(sales_quantity))
print("单价：", unit_price, type(unit_price))


print("\n第三部分：模拟 input() 并转换")

quantity_text = "3"
price_text = "19.9"

quantity = int(quantity_text)
price = float(price_text)
total = quantity * price

print("原始输入类型：", type(quantity_text), type(price_text))
print("转换后类型：", type(quantity), type(price))
print(f"销售额：{total:.2f} 元")


print("\n第四部分：print() 格式")

print(product_name, quantity, price, sep=" | ")
print(f"{product_name} × {quantity} = {total:.2f} 元")


print("\n第五部分：购物小票")


def print_receipt(product, quantity, unit_price):
    """输出购物小票并返回总额。"""
    total = quantity * unit_price

    print("=" * 30)
    print("       商务数据训练商店")
    print("-" * 30)
    print(f"商品：{product}")
    print(f"数量：{quantity}")
    print(f"单价：{unit_price:.2f} 元")
    print(f"合计：{total:.2f} 元")
    print("=" * 30)
    return total


receipt_total = print_receipt(product_name, quantity, price)
print(f"函数返回值：{receipt_total:.2f} 元")


print("\n第六部分：植树证书")


def print_tree_certificate(nickname, plant_name, certificate_id):
    """根据三个参数输出证书。"""
    print("╔" + "═" * 32 + "╗")
    print("║          植 树 证 书           ║")
    print("╠" + "═" * 32 + "╣")
    print(f"  申请人：{nickname}")
    print(f"  植物：  {plant_name}")
    print(f"  编号：  {certificate_id}")
    print("╚" + "═" * 32 + "╝")


print_tree_certificate("小数同学", "梭梭树", "TREE-2026-0722")


print("\n第七部分：解释与自检")

# TODO 13：
# = 用于赋值，让变量名指向一个值；== 用于比较两个值是否相等，结果为 True 或 False。

# TODO 14：
# "19.9" 是字符串，字符串乘整数表示重复文本，因此 "19.9" * 3 得到的是重复字符串，
# 不是数值乘法。应先使用 float("19.9") 转换为浮点数。

# 自检：
# [x] 所有变量名合法且含义明确。
# [x] quantity 是 int，price 与 total 是 float。
# [x] 小票金额由程序计算，不是手工填写。
# [x] 证书变化字段通过参数传入。
# [x] 程序可以从上到下一次运行。
