Python Forum
Not understanding dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not understanding dictionaries
#2
use keys to get count:
>>> stuff={
...     'rope':1,'torch':6,'gold coins':45,'dagger':1,'arrow':12
... }
>>> def display_inventory(inventory):
...     print(f"\nInventory: Total items: {len(inventory.keys())}\n")
...     for key, value in inventory.items():
...         print(f"{key}: {value}")
... 
>>> display_inventory(stuff)

Inventory: Total items: 5

rope: 1
torch: 6
gold coins: 45
dagger: 1
arrow: 12
Reply


Messages In This Thread
Not understanding dictionaries - by gr3yali3n - Jun-18-2020, 08:22 PM
RE: Not understanding dictionaries - by Larz60+ - Jun-18-2020, 08:56 PM
RE: Not understanding dictionaries - by bowlofred - Jun-18-2020, 08:56 PM
RE: Not understanding dictionaries - by gr3yali3n - Jun-20-2020, 03:10 AM
RE: Not understanding dictionaries - by buran - Jun-20-2020, 03:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 3,326 Aug-11-2018, 06:45 PM
Last Post: barles

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020