Search code examples
pythonvariablesprintingeval

Print eval not working to print variable from string


class ddx:
  micro = ["iron deficiency (late)", "chronic disease (late)", "thalassemias", "sideroblastic anemia", "lead poisoning"],
  normo = ["normo"],
  macro = ["macro"]

anemiaType = random.choice(["micro", "normo", "macro"])
print eval('ddx.' + anemiaType)

Let's say the random.choice is "micro"

I'd like to be able to print ddx.micro

I know I could create a whole bunch of if/else statements to accomplish this, but it seems very clunky/not super readable.

Anyone have any suggestions?


Solution

  • Nvm I just realized I made the dumbest python 3 mistake of all time - writing print instead of print()