Record#
- Today I learned about two-dimensional dictionaries. I used to confuse the concept of arrays (lists) and dictionaries.
- Arrays are defined using square brackets and accessed using indexes.
- 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.
- Adding data to a dictionary does not require appending; as long as the keys are different, the data will be automatically added.
- 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: