1

At the moment my code has multiple if statements that are similar (see below) and i was wondering if there was a way to make a variable or something that can change based on what comes out of the random.choice?

So if it landed on druid instead of checking if the result was barbarian then moving onto the next block of code it would just take druid from the random.choice output and change the import for a single block of code accordingly

sorry if this is worded badly, it's hard for me to convey what i mean, i can elaborate if needed

def randclass():
    return random.choice(class_dict[chosenrace])

if randclass() == "Barbarian":
    print(chosenrace, "Barbarian")
    if fullconfirm == "yes":
        from barbarianfullbuild import barbarianrandsubclass, barbarianrandbackground, barbarianskills
        print(barbarianrandsubclass)
        print(barbarianrandbackground)
        print(barbarianskills)

if randclass() == "Druid":
    print(chosenrace, "Druid")
    if fullconfirm == "yes":
        from druidfullbuild import druidrandsubclass, druidrandbackground, druidskills
        print(druidrandsubclass)
        print(druidrandbackground)
        print(druidskills)
burgernado
  • 17
  • 3
  • "change the import" are you trying to unload imports? If so why, are they extremely expensive? – Kraigolas Jan 09 '22 at 18:55
  • Instead of having `class_dict[chosenrace]` be a list of strings, have you considered making it be a list of dicts, where each dict has properties like name, subclass, background, and skills? That way, you could simply take those out of whatever `randclass()` returns rather than using an `if` tower. – Green Cloak Guy Jan 09 '22 at 18:56
  • 1
    Why do you have different classes for Barbarians and Druids? Don't they share a common interface? – Thomas Weller Jan 09 '22 at 19:00
  • It's weird that you call `randclass` twice, so it's possible that both or neither of the `if` conditions are true. Is that intended? – Kelly Bundy Jan 09 '22 at 19:03
  • Does this answer your question? [import module from string variable](https://stackoverflow.com/questions/8718885/import-module-from-string-variable) – JonSG Jan 09 '22 at 19:09

2 Answers2

0

You can use python's dict as a hash-map in order to avoid those ifs:

def randclass():
    return random.choice(class_dict[chosenrace])

def import_barbarian():
    print(chosenrace, "Barbarian")
    if fullconfirm == "yes":
        from barbarianfullbuild import barbarianrandsubclass, barbarianrandbackground, barbarianskills
        print(barbarianrandsubclass)
        print(barbarianrandbackground)
        print(barbarianskills)

def import_druid():
    print(chosenrace, "Druid")
    if fullconfirm == "yes":
        from druidfullbuild import druidrandsubclass, druidrandbackground, druidskills
        print(druidrandsubclass)
        print(druidrandbackground)
        print(druidskills)

import_races = {
    "Barbarian": import_barbarian,
    "Druid": import_druid
    }

import_races[randclass()]()
Joseph Pena
  • 167
  • 2
  • 8
-1

Try this code, You need to write it only once and it will work for all the random cases.

def randclass():
    return random.choice(class_dict[chosenrace])

a = randclass()
print(chosenrace,a)
b = a.lower()
if fullconfirm == "yes":
   exec(f"from {b}fullbuild import {b}randsubclass, {b}randbackground, {b}skills")
   exec(f"print({b}randsubclass)")
   exec(f"print({b}randbackground)")
   exec(f"print({b}skills)")

exec() is a function that executes a line of code in main passed as a string, it's a good workaround for situations like this.