Trying to use original input with functions

I am new to python and I am trying to create a command for a program with raw_input and functions. For some reason it does not work. Here is the code I tested:

raw_input() def test(): print "hi, this will be amazing if it works" 
+4
source share
4 answers

raw_input will block until you enter something. When a new line is received (pressing the user button), the value will be returned and can be saved. It seems you never tried to name your function test . You might want to try something like this (I can explain if you need it)

 name = raw_input("What is your name: ") def test(username): print "Hi %s, this will be amazing if it works" % (username,) test(name) 

Based on your other comments, this is a safe way:

 # Define two functions test() and other() def test(): print "OMG, it works..." def other(): print "I can call multiple functions" # This will be to handle input for a function we don't have def fail(): print "You gave a bad function name. I only know about %s" % (", ".join(funcs.keys())) # This is a dictionary - a set of keys and values. # Read about dictionaries, they are wonderful. # Essentially, I am storing a reference to the function # as a value for each key which is the value I expect the user to ender. funcs = {"test": test, "other": other} # Get the input from the user and remove any trailing whitespace just in case. target = raw_input("Function to run? ").strip() # This is the real fun. We have the value target, which is what the user typed in # To access the value from the dictionary based on the key we can use several methods. # A common one would be to use funcs[target] # However, we can't be sure that the user entered either "test" or "other", so we can # use another method for getting a value from a dictionary. The .get method let us # specify a key to get the value for, as wel as letting us provide a default value if # the key does not exist. So, if you have the key "test", then you get the reference to # the function test. If you have the key "other", then you get the reference to the # function other. If you enter anything else, you get a reference to the function fail. # Now, you would normally write "test()" if you wanted to execute test. Well the # parenthesis are just calling the function. You now have a reference to some function # so to call it, you have the parenthesis on the end. funcs.get(target, fail)() # The above line could be written like this instead function_to_call = funcs.get(target, fail) function_to_call() 
+6
source

You need to assign raw_input () output something like this ( documentation ):

 s = raw_input('--> ') 

Also your code really works (surprisingly true?) You just defined a function but did not call it. Add this to the end of your Python file (no indentation, everything left):

 test() 
+3
source

You need to assign the value of raw_input () to something, like a variable, if you want to use the input.

Then remember that everything inside your def (indentation) will not be executed until you call def. Probably why nothing works, you are not calling def.

Just name it by putting test () somewhere and it will print.

The function does not start until it is called. The code that executes when it is called is indented under "def". You can put code snippets in a function that you can call many times, without having to write it every time.

0
source
 input = raw_input() def test(): print "hi, this will be amazing if it works" if input == "test": test() 
0
source

All Articles