+ 2

How do I call a function from a string?

I'm trying to write a code for python. I am trying to get the code to call a function based on a raw_input string from my main code. im a beginner so this code is pretty elementary. basically: print ("how was your day?") user_resp = raw_input("") if user_resp == str("good"): #call I don't know what to put here....... def response("good"): print ":)" def response("bad"): print ":(" #this is where I want the code to call a function because I want different str("good") and a str("bad") responses.

25th Sep 2017, 5:38 PM
Stef
Stef - avatar
2 Réponses
+ 3
You don't need to call functuon for such a thing: print ("How was your day?") user_resp = input("") if user_resp == str("good"): print (":)") else: print (":(") You can use elif to widen the options. If you want to do it explicitly this is the way: def good(): print (":)") def bad(): print (":(") print ("How was your day?") user_resp = input("") if user_resp == str("good"): good() else: bad()
25th Sep 2017, 7:11 PM
Leno
Leno - avatar
+ 2
Here is the code that does what you want. All explanation is in comments. If you want more explanation tell and I'll help. https://code.sololearn.com/c43mY1GvbovA/?ref=app
25th Sep 2017, 6:34 PM
Tim Thuma
Tim Thuma - avatar