-1
if(input == "Karma Score"):
    print("Your Karma score is {}.format(charKarma) + ".")

This may be an odd question, and I am new to python so I'm stumped. I set myself a goal of finishing a text-based adventure game that features a 'Karma" system. It's basically a watered-down version of the Fallout series' Karma system.

I need help figuring out how to callback to a variable value when requested from the console.

A simplified version of my spaghetti code is:

if(Input == "Print Variable")
    print("variable")

Thanks a bunch for your time.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Fillbert
  • 1
  • 1
  • 1
    um, what do you mean by *callback* here exactly? I don't think you mean an actual callback, which is basically an argument to a function that is another function which will be used internally, (called back so to speak) – juanpa.arrivillaga Jun 05 '20 at 06:11
  • 1
    Your Q is quite unclear but if I understand correcty [this](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string) answers it. – snatchysquid Jun 05 '20 at 06:11
  • In any case, it *sounds* to me like what you mean is you want to be able to refer to a variable based on a user input. Don't do that. That is bad design. [Keep data out of your variable names](https://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html). And you should stop thinking in terms of "variables" anyway, and in terms of objects. Create a mapping (a `dict`) of `str` objects to whatever other objects you require, and use that. – juanpa.arrivillaga Jun 05 '20 at 06:13
  • @juanpa.arrivillaga I'll keep that advice in mind for the future. That was just the way I learned. But if the way you suggest is the objectively better design, I'll try and learn data definition in that way instead. Thanks! – Fillbert Jun 05 '20 at 06:31
  • Keep it in mind for now, you can use that *right now*. – juanpa.arrivillaga Jun 05 '20 at 06:35
  • @juanpa.arrivillaga I'm not in the mood to re-write 400 lines of code at 3 am, but maybe tomorrow. I basically comprised my entire Text-based game around defining a Variable to select a dialogue or action option. May have been a huge rookie mistake, but mistakes are how I learn, and I'm always up to learn. – Fillbert Jun 05 '20 at 06:40

1 Answers1

0

Notwithstanding that it might be bad design as @juanpa.arrivillaga noted in the comments, it can be done quite easily in Python.

Python conceptually holds variables and their values in dictionaries, which you can retrieve using the built-in functions globals() or locals(). In your case globals() is probably what you want. So, you could print out the value of a variable karma like this:

print( globals()["karma"] )

Or, if I understand your intentions correctly, here is how it might look in context:

user_input = input("command$ ")
words = user_input.split()
if words[0] == "print":
    var_name = words[1]
    value = globals()[var_name]
    print(value)

Just to be complete here: if the variable is defined in another module, either use getattr(module, var_name) or module.__dict__[var_name]. So, the following works, too:

import math
print( getattr(math, "pi") )
pritn( math.__dict__["pi"] )

The __dict__ here is basically what globals() returns for the current module.

Tobias
  • 1,321
  • 6
  • 11