4

I am new to python and I'm trying to make a command kind of thing for a program with raw_input and functions. For some reason it hasn't been working. Here is the code I've been testing with:

raw_input()

def test():
    print "hi, this will be amazing if it works"
Bart
  • 19,692
  • 7
  • 68
  • 77
mrkzam123
  • 43
  • 1
  • 1
  • 4

4 Answers4

6

raw_input will block until you type something in. When a newline is received (user presses enter) the value will be returned and can be stored. It does not appear you are ever trying to call your function test. Perhaps you want to try something like this (I can explain further 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 the safe way to do this:

# 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's 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()
sberry
  • 128,281
  • 18
  • 138
  • 165
  • This is a good answer overall, but many parts of it would probably go over the head of most 11-year-olds. – Anorov May 01 '13 at 23:20
  • 2
    Tried adding some commentary. – sberry May 02 '13 at 01:34
  • I'm just jumping back into this question, but you could use Python 2.7's input() function which evaluates the input. I think is this what you were alluding to by the "safe way" – Jason Sperske May 02 '13 at 02:53
  • well imma copy paste this into a notepad and try to read all those comments, thanks for the great answer. ;). – mrkzam123 May 04 '13 at 11:18
3

You need to assign the output of raw_input() to something like so (documentation):

s = raw_input('--> ')

Also your code does work (amazing right?) you just defined a function but didn't call it. Add this to the end of your Python file (with no indentation, all the way to the left):

test()
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • how does a function work? I mean can i make it so it does nothing until specified w/ raw_input? – mrkzam123 May 01 '13 at 22:08
  • Defining a function and calling that function are separate steps. @sberry's answer shows all of this working together in a single script – Jason Sperske May 01 '13 at 22:10
0

You have 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 (the indented part) won't execute until you call the def. That's probably why nothing works, you're not calling the def.

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

A function doesn't run until you call it. The code that executes when it is called is the indented part under the 'def'. You can put pieces of code in a function that you might call many times, without having to write it again each time.

John
  • 53
  • 6
0
input = raw_input()

def test():
    print "hi, this will be amazing if it works"

if input == "test":
    test()
Patashu
  • 21,443
  • 3
  • 45
  • 53