Answer the question
In order to leave comments, you need to log in
Is it possible to change the called functions depending on the content of the string?
I can't really describe what I want, but I'll try.
def num(Number):
if Number == 1:
print("Hi")
elif Number == 2:
print("hello")
elif Number == 1 2 3;
print("Hi hello nice" )
elif Number == 1-3:
print("Hi-nice")
Answer the question
In order to leave comments, you need to log in
In general, such things are done through a dictionary.
If you just want to make a template string, then you can use replace via replace, bypassing all the key-value pairs in your dictionary.
d = {1: 'Hi', 2: 'hello', 3: 'nice'}
def foo(s):
for key, value in d.items():
s = s.replace(str(key), value)
print(s)
foo('1')
foo('2')
foo('1 2 3')
foo('1-3')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question