Day 046 - Project 46 : MokéBeasts Mokédex

If you have any questions, comments or issues with this project please post them here!

For day 46 how do you use a conditional in a 2d dictionary like if the fakemon type is ghost make the color black and so on

fakedesk ={}
def stylePrint()
for key, value in fakedesk.items():
print (key,end=" : ")
for subkey, subvalue in value():
print(subkey, subvalue ,end= "  |  ")

#if...the type is ghost ill change the color but what should I call ex..
#if (fakédesk["FakéMon Type"])=="Ghost":
      print("\033[30m",end="") 

while True:
name=input("fakemon name")
type= input("fakemon type" )
hp=input("fakemon hp")
fakedesk[name]={"type" : type,"hp": hp}
stylePrint()

Hey @Rocko,

Could you describe better what you want to do? I think I’m know what you mean, but I get confused.

I want to change the color of the specific pokemon type to a specific color by the specific type! see the code here I think I’ll understand

#Project FakéMon! No! copyright! intended! 
import os
print("\033[31mFak\033[33mé\033[36mMon!\033[0m")
print()
print("Gotta Faké'ém All😶‍🌫️")
print()
fakédesk={}
def stylePrint():
  for key, value in fakédesk.items():
    print (key, end=" : ")
    print("heres the brief info of you'r \033[31mFak\033[33mé\033[36mMon!\033[0m")
    if fakédesk["FakéMon Type"]=="Fire":
      print("\033[31m",end="")
    elif fakédesk["FakéMon Type"]=="Water":
      print("\033[34m",end="")
    elif fakédesk["FakéMon Type"]=="Earth":
      print("\033[32m",end="")
    elif fakédesk["FakéMon Type"]=="Mud":
      print("\033[30m",end="")
    elif fakédesk["FakéMon Type"]=="Poison":
      print("\033[35m",end="")
    elif fakédesk["FakéMon Type"]=="Electric":
      print("\033[33m",end="")
    else:
      print("\033[37m",end="")
    print()
    for subkey, subvalue in fakédesk.items():
      print(subkey, subvalue , end="  |  ")
while True:
  name=input("FakéMon Name: ") 
  type=input("FakéMon Type: " )
  ability=input("FakéMon Special Ability: ")
  hiddenPower=input("FakéMon Hidden Power: ") 
  hp=input("FakéMon Hp: ")
  mp=input("FakéMon Mp: ")
  fakédesk[name]={"FakéMon Type":type,"FakéMon Special Ability":ability,"FakéMon Hidden Power":hiddenPower,"FakéMon Hp":hp,"FakéMon Mp":mp}
  stylePrint()
  tryAgain=input("would you like to add more beasts into your Fakédesk?\n ~").lower()
  if tryAgain=="y":
    continue
  else:
    break

I suppost you want to change the text color when printed, to do that I recommend this :

from termcolor import colored

print(colored("Hello!","red"))

I prefer printing colored text like this.

You can reduce that whole:

  for key, value in fakédesk.items():
    print (key, end=" : ")
    print("heres the brief info of you'r \033[31mFak\033[33mé\033[36mMon!\033[0m")
    if value["FakéMon Type"]=="Fire":
      print("\033[31m",end="")
    elif value["FakéMon Type"]=="Water":
      print("\033[34m",end="")
    elif value["FakéMon Type"]=="Earth":
      print("\033[32m",end="")
    elif value["FakéMon Type"]=="Mud":
      print("\033[30m",end="")
    elif value["FakéMon Type"]=="Poison":
      print("\033[35m",end="")
    elif value["FakéMon Type"]=="Electric":
      print("\033[33m",end="")
    else:
      print("\033[37m",end="")

to:

for key, value in fakédesk.items():
  print (key, end=" : ")
  print("heres the brief info of you'r \033[31mFak\033[33mé\033[36mMon!\033[0m")
  color = 37;
  match value["FakéMon Type"]:
    case "Fire":
      color = 31
    case "Water":
      color = 34
    case "Earth":
      color = 32
    case "Mud":
      color = 30
    case "Poison":
      color = 35
    case "Electric":
      color = 33
    case _:
      color = 37
  print("\033[" + color + "m", end="") #or use fstring f"\033[{color}m"

It‘s not shorter, but much more readable.

Also, you could directly pass the fakemon to the styleprint function if you only wanted to print the latest one and loop in the while True loop to repeat that styleprint for each item in fakedesk.

Edited to include fstring comment. It looks less readable, but maybe you prefer that one.

Edited again:
That only works with python 3.10 and higher.

If you want lower, you can use this:

for key, value in fakédesk.items():
  print (key, end=" : ")
  print("heres the brief info of you'r \033[31mFak\033[33mé\033[36mMon!\033[0m")
  types = {"Fire":31, "Water":34, "Earth":32, "Mud":30, "Poison":35, "Electric":33}
  color = types[value["FakéMon Type"]]
  print("\033[" + color + "m", end="")

Also, just noticed you likely meant to use value instead of fakédesk, so I corrected that too.

@Elus ,
thank you that was what I wanted! I’m a newbie so didn’t knew the match value system until now thanks for the new knowledge most appreciated! and the last one I can’t use as I haven’t decided on the types yet I’m just putting some types with some colors the user can make other types so using the dictionary here will crash the program if the user decided to choose out of the box lol and ya f string is easier to write lol just 1 quote works for the whole thing thank you, bro.

and 1 last question in the match value [“fakemon type”]:
is this indented?

I’m somehow getting an invalid syntax error when I try to imply the match value it says invalid syntax

here’s the code,

#Project FakéMon! No! copyright! intnted!
import os
print(“\033[31mFak\033[33mé\033[36mMon!\033[0m”)
print()
print(“Gotta Faké’ém All😶‍🌫️”)
print()
fakédesk={}
def stylePrint():
print(f"FakéMon Name\tFakéMon Type\tFakéMon Special Ability\tFakéMon Hidden Power\tFakéMon Hp\tFakéMon Mp")
for key, value in fakédesk.items():

color = 37;
match value["FakéMon Type"]:
  case "Fire":
    color = 31
  case "Water":
    color = 34
  case "Earth":
    color = 32
  case "Mud":
    color = 30
  case "Poison":
    color = 35
  case "Electric":
    color = 33
  case _:
    color = 37
print("\033[" + color + "m", end="")
print (f"""{key:^16}|{value["FakéMon Type"]:^14}|{value["FakéMon Special Ability"]:^20}|{value["FakéMon Hidden Power"]:^22}|{value["FakéMon Hp"]:^18}|{value["FakéMon Mp"]:^16}""") 

while True:
print(“Add your FakéMon!”)
name=input("FakéMon Name: ")
type=input("FakéMon Type: " )
ability=input("FakéMon Special Ability: ")
hiddenPower=input(“FakéMon Hidden Power: “)
hp=input(“FakéMon Hp: “)
mp=input(“FakéMon Mp: “)
fakédesk[name]={“FakéMon Type”:type,“FakéMon Special Ability”:ability,“FakéMon Hidden Power”:hiddenPower,“FakéMon Hp”:hp,“FakéMon Mp”:mp}
print(”…beep boop…”)
print(”…”)
print(”…”)
print(“heres the brief info of you’r \033[31mFak\033[33mé\033[36mMon!\033[0m”)
stylePrint()
tryAgain=input(“would you like to add more beasts into your Fakédesk?\n ~”).lower()
if tryAgain==“yes”:
continue
else:
print(“sad to see you go…come back later!”)
break

Try running this: `import sys;print(sys.version)

`.

If the result is something below 3.10, you‘ll have to use the alternative I posted earlier.

Whether you use a match statement or the alternative (as it is), you’d still have to know the types in advance.

In fact, with the alternative you could adjust the code slightly to update that dictionary with new types at runtime, as long as you stored it in some more globally accessible place (ie.: if you had a FakeDex class, you‘d likely want to have a static member types that lists all types and corresponding information, like color, weaknesses, and strengths, which you could then update via a static FakeDex.addType() method).

@Elus ,
I think I have python 3 installed wait let me see
edit,
yup 3.8

here’s the code that’s I’m using without knowing the types

#Project FakéMon! No! copyright! intnted!
import os
print(“\033[31mFak\033[33mé\033[36mMon!\033[0m”)
print()
print(“Gotta Faké’ém All😶‍🌫️”)
print()
fakédesk={}
def stylePrint():
print(f"FakéMon Name\tFakéMon Type\tFakéMon Special Ability\tFakéMon Hidden Power\tFakéMon Hp\tFakéMon Mp")
for key, value in fakédesk.items():
if value[“FakéMon Type”]==“Fire”:
print(“\033[31m”,end=“”)
elif value[“FakéMon Type”]==“Water”:
print(“\033[34m”,end=“”)
elif value[“FakéMon Type”]==“Earth”:
print(“\033[32m”,end=“”)
elif value[“FakéMon Type”]==“Mud”:
print(“\033[30m”,end=“”)
elif value[“FakéMon Type”]==“Ghost”:
print(“\033[35m”,end=“”)
elif value[“FakéMon Type”]==“Electric”:
print(“\033[33m”,end=“”)
else:
print(“\033[37m”,end=“”)
print (f"“”{key:^16}|{value[“FakéMon Type”]:^14}|{value[“FakéMon Special Ability”]:^20}|{value[“FakéMon Hidden Power”]:^22}|{value[“FakéMon Hp”]:^18}|{value[“FakéMon Mp”]:^16}“”")
while True:
print(“Add your FakéMon!”)
name=input("FakéMon Name: ")
type=input("FakéMon Type: " )
ability=input(“FakéMon Special Ability: “)
hiddenPower=input(“FakéMon Hidden Power: “)
hp=input(“FakéMon Hp: “)
mp=input(“FakéMon Mp: “)
fakédesk[name]={“FakéMon Type”:type,“FakéMon Special Ability”:ability,“FakéMon Hidden Power”:hiddenPower,“FakéMon Hp”:hp,“FakéMon Mp”:mp}
print(”…beep boop…”)
print(”…”)
print(”…”)
print(“heres the brief info of you’r \033[31mFak\033[33mé\033[36mMon!\033[0m”)
stylePrint()
print(”\033[0m”)
tryAgain=input(“would you like to add more beasts into your Fakédesk?\n ~”).lower()
if tryAgain==“yes”:
continue
else:
print(“sad to see you go…come back later!”)
break

and thank you for all the help you be the most helpful person to me on this site so far! thanks a lot!! like really :pray::people_hugging:

Yeah, 3.8 is below 3.10 (that‘s a ten, not a one with an extra decimal zero). Match only works with versions higher than or equal to 3.10.

You‘ll have to use the alternative I suggested. It‘ll do exactly the same as the match statement, but it‘s compatible with your version 3.8.

As a side note, I think you mean „no copyright infringement intended“ in the comment above your code.

1 Like

@Elus
haha ya, thanks again brother.