-1

I have different valued variables not in an array; e.g.

a = "united states"
b = "canada" 
c = "mexico"

I also have similarly named variables with the same suffix

a_pop = 0
b_pop = 0
c_pop = 0

I have made a function to select randomly a, b or c, but is there any way to edit the value of {selected value}_pop (if a then edit a_pop's value, b to b_pop etc.) in a function without changing the previous variables of or using an tedious if/else ladder?

For example, a is selected. Now (with a selected) I want to add 1 to a_pop's value.

Georgy
  • 9,972
  • 7
  • 57
  • 66

1 Answers1

2

A little different from your original code, but more Pythonic:

names = {
    'a' : "united states"
    'b' : "canada" 
    'c' : "mexico"
}

values = {
    'a' : 0
    'b' : 0
    'c' : 0
}

Picking a random variable var...

values[var] += 1

Hope it's clear how this works for your needs. If not feel free to ask for clarification.

deceze
  • 491,798
  • 79
  • 706
  • 853
Josh Friedlander
  • 8,209
  • 4
  • 32
  • 67