Reputation: 1067
I'm having some trouble figuring out something. Let's say that I have a list of strings (a csv,txt,or something like that) that looks something like this:
1)Set Car color = red
2)Set Radio-Volume = 50%
3)Car AC temp = 20°C
And I want each of those lines to execute some functions that I have created. Let's say that I have created some functions that use the values from the string above. Ex:
def set_color(colorvalue):
car_color= colorvalue
print("The car has been set to: ",colorvalue)
def radio_vol(vol):
#do something here
def set_car_ac_to(deg):
#do something else here
etc,you get the idea. What is the best approach, so that when parsed, the csv,txt file containing the lines of text, to call their specific function with the given parameter. How can I assign a function to a string and use it's specific param. Thank you
Upvotes: 0
Views: 50
Reputation: 2855
Use a dict
:
actions = {"Set Car color": set_color,
"Set Radio-Volume": radio_vol, ...}
command = "Set Car color"
argument = "red"
actions[command](argument)
Upvotes: 1