0

I want to read in a file that contains a variable name that is defined in my code i.e. (Variable1 = 5 and Variable2 = 6).

For example, my file may look something like this:

Variable1
Variable2

I can read those names into a list (lets call it input_list)

Now I want to use the value of that variable in another function ie:

add(input_list[0], input_list[1])

However, it is reading the input as strings Variable1 and Variable2, but I want it to read in the values as ints of 5 and 6)

I know this sounds confusing, but hopefully someone can understand what I am trying to do (if it is even possible)

SSB
  • 69
  • 7
  • @cᴏʟᴅsᴘᴇᴇᴅ, I don't think that is a good duplicate. While it maybe a better solution to the OP's problem (whatever that is), it is not a duplicate to the question. – Stephen Rauch Feb 20 '18 at 05:05
  • You can cast variables to int `map(int, input_list)` – Nikitka Feb 20 '18 at 05:14
  • @COLDSPEED I'm not sure the question you marked as duplicate is the same thing. Although I could probably use a dictionaries, I don't think that would be efficient at all, as I am grabbing the variable declarations from yet another file – SSB Feb 20 '18 at 05:17
  • @Nikitka Thanks for the suggestion but casting won't work in this case either has input_list will contain "Variable1" and "Variable2" – SSB Feb 20 '18 at 05:25
  • 1
    @user3326870 Do you have variable names in the file? If so, you can get variable value like this `eval(input_list[0])` – Nikitka Feb 20 '18 at 05:28
  • @Nikitka Yes! This was exactly what I was looking for Thank You!! – SSB Feb 20 '18 at 05:48

0 Answers0