🐸

Inventory Check

Python Basicspython-basics-28-inventory-check
Reward: 135 XP
|

Inventory Check

Before leaving the dungeon, Hoppy needs to make sure he has everything.

Use len() to count items and the in operator to check if something is inside.

Checking Tools

backpack = ["Map", "Compass", "Key"]

# How many items?
count = len(backpack) # 3

# Is "Key" in the backpack?
has_key = "Key" in backpack # True

# Is "Gold" in the backpack?
has_gold = "Gold" in backpack # False

Your Mission

1
The Bag

We have a list items hidden from you.

2
Count

Create a variable total that stores the length of items.

3
Search

Create a variable has_potion that is True if "Potion" is in the list, and False otherwise.

4
Report

Print total and has_potion.

Suggested Solution
Click to expand

in works like a question, giving you True or False.

# Assuming items is defined
total = len(items)
has_potion = "Potion" in items
print(total)
print(has_potion)
Advanced Tips
Want more? Click to expand

You can use not in to check if something is MISSING.

if "Map" not in backpack:
  print("We are lost!")
pymain.py
Loading...
Terminal
Terminal
Ready to run...