بتل کد Battle Codeدارد باز می‌شود…

جواب «Flag 6: Hidden Price» چالش 6 از جلسه‌ی 61

سؤال

یک فروشگاه با فیلد پنهان price در فرم؛ مهاجم آن را 1 می‌کند. سرور را درست بنویس. ورودی: اول n، بعد n خط <item> <price_from_form> <qty>. جدول واقعی: PRICES = {"ball": 30, "rocket": 50, "robot": 80}. برای هر خط، قیمت را از جدول بگیر (نه از فرم)؛ اگر قیمت فرم با جدول فرق داشت اول چاپ کن tampered: <item>. بعد total <item>: <price * qty>. اگر کالا در جدول نبود فقط unknown item: <item>. در پایان grand total: <جمع همه>؛ و اگر حتی یک دست‌کاری بود، خط آخر BC{never_trust_the_form}.

راهنما

دو شمارنده: grand و tampered. قیمت فرم را فقط برای مقایسه بخوان.

یک جواب درست

PRICES = {"ball": 30, "rocket": 50, "robot": 80}
n = int(input())
grand = 0
tampered = 0
for i in range(n):
    item, form_price, qty = input().split()
    if item not in PRICES:
        print("unknown item:", item)
        continue
    price = PRICES[item]
    if int(form_price) != price:
        print("tampered:", item)
        tampered += 1
    total = price * int(qty)
    grand += total
    print("total " + item + ": " + str(total))
print("grand total:", grand)
if tampered > 0:
    print("BC{never_trust_the_form}")

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن