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

جواب «Pick It Up» چالش 3 از جلسه‌ی 36

سؤال

وسیله‌ها اضافه شدند! همان بازی مرحله‌ی قبل را ادامه بده و دو دستور جدید بساز:
take: اگر در اتاق فعلی وسیله‌ای بود، آن را به لیست bag اضافه کن، از items پاکش کن و چاپ کن You took the key (با اسم واقعی)؛ وگرنه Nothing here.
bag: خود لیست bag را چاپ کن.
حرکت‌ها، پیام‌هایشان و quit (با Bye!) مثل قبل هستند.

راهنما

برای take بررسی کن current in items. بعد bag.append(items[current]) و del items[current]. یادت باشد پیام را «قبل» از پاک کردن بسازی یا اسم وسیله را در یک متغیر نگه داری.

یک جواب درست

rooms = {
    "hall": {"north": "kitchen", "east": "library"},
    "kitchen": {"south": "hall"},
    "library": {"west": "hall", "north": "garden"},
    "garden": {"south": "library"},
}
items = {"kitchen": "key", "library": "map"}
bag = []
current = "hall"
while True:
    cmd = input()
    if cmd == "quit":
        break
    if cmd == "take":
        if current in items:
            thing = items[current]
            bag.append(thing)
            del items[current]
            print(f"You took the {thing}")
        else:
            print("Nothing here")
    elif cmd == "bag":
        print(bag)
    elif cmd in rooms[current]:
        current = rooms[current][cmd]
        print(f"You are in the {current}")
    else:
        print("You can't go that way")
print("Bye!")

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