二手产品经理

二手产品经理

THIS IS RENO

Two-dimensional dictionary - 46 days - Learn Python online for 100 days.

Record#

Catbox

  1. Today I learned about two-dimensional dictionaries. I used to confuse the concept of arrays (lists) and dictionaries.
  2. Arrays are defined using square brackets and accessed using indexes.
  3. Dictionaries are defined using curly brackets and accessed using key-value pairs. The values in a dictionary appear in pairs, with both the key and value present.
    • Arrays are suitable for ordered collections of elements, accessed and manipulated using indexes.
    • Dictionaries are suitable for unordered collections of key-value pairs, accessed and manipulated using keys.
    • Two-dimensional arrays are used to represent two-dimensional data structures, accessed using two indexes.
    • Two-dimensional dictionaries are used to represent data with a row-column structure, accessed using two keys.
  4. Adding data to a dictionary does not require appending; as long as the keys are different, the data will be automatically added.
  5. Today's exercise is to record and output information about Pokémon pets.

CODE#

import os

print("🌟MokeBeast Generator🌟")

mokelist = {}
again = "y"


def prettyPrint():
  print(f"{'Name': ^10}", end=" | ")
  print(f"{'Type': ^10}", end=" | ")
  print(f"{'HP': ^10}", end=" | ")
  print(f"{'MP': ^10}", end=" | ")
  print()
  for key, value in mokelist.items():
    print(f"{key: ^10}", end=" | ")
    for subkey, subvalue in value.items():
      print(f"{subvalue: ^10}", end=" | ")
    print()


while again == "y":
  os.system("clear")
  print("Add Your Beast!")
  name = input("Name > ")
  type = input("Type > ")
  hp = input("HP > ")
  mp = input("MP > ")
  mokelist[name] = {"type": type, "hp": hp, "mp": mp}
  prettyPrint()
  print()
  again = input("again: y / n > ")

Translation:

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.