جواب «Step 4: formatted invoice» چالش 4 از جلسهی 6
سؤال
نام، قیمت واحد و تعداد را بخوان. مثل گام ۳ تخفیف را حساب کن، سپس ۹٪ مالیات روی مبلغ پس از تخفیف بگیر. فاکتور را اینطور چاپ کن: برچسب در ستون ۱۲ کاراکتری چپچین، مبلغ در ستون ۱۲ کاراکتری راستچین با جداکنندهی هزارگان و دو رقم اعشار. خط اول INVOICE: <name> و خط دوم ۲۴ خط تیره است. مثال برای laptop، 1250، 2:INVOICE: laptop------------------------subtotal 2,500.00discount 250.00tax 202.50total 2,452.50
راهنما
هر خط مبلغ را با f-string و مشخصههای <12 و >12,.2f بساز. مالیات = (subtotal − discount) × 0.09.
یک جواب درست
TAX_RATE = 0.09
name = input().strip()
price = float(input())
qty = int(input())
subtotal = price * qty
if subtotal >= 1000:
rate = 0.10
elif subtotal >= 500:
rate = 0.05
else:
rate = 0
discount = subtotal * rate
tax = (subtotal - discount) * TAX_RATE
total = subtotal - discount + tax
print(f"INVOICE: {name}")
print("-" * 24)
print(f"{'subtotal':<12}{subtotal:>12,.2f}")
print(f"{'discount':<12}{discount:>12,.2f}")
print(f"{'tax':<12}{tax:>12,.2f}")
print(f"{'total':<12}{total:>12,.2f}")
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن