2013-05-01 31 views
4

我是新来的python,我试图做一个与raw_input和功能的程序的事情的命令类。由于某种原因,它一直没有工作。这是我一直在测试代码:试图使用功能的原始输入

raw_input() 

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

你试图做什么? – fgb 2013-05-01 22:04:53

回答

3

您需要的的raw_input()的输出分配给像这样(documentation):

s = raw_input('--> ') 

而且你的代码不工作(惊人对吗?)你只是定义了一个函数,但没有调用它。添加到您的Python文件的末尾(无压痕,一路到左):

test() 
+0

函数是如何工作的?我的意思是我可以做到这一点,直到指定w/raw_input它什么都不做。 – mrkzam123 2013-05-01 22:08:48

+0

定义一个函数并调用该函数是分开的步骤。 @ sberry的回答显示所有这些在一个脚本中一起工作 – 2013-05-01 22:10:04

6

raw_input将阻塞,直到你输入的东西当接收到一个换行符(用户按下输入)的值。将被退回并且可以被存储。看起来你并没有试图打电话给你的功能test。也许你想尝试这样的事情(我可以​​进一步解释,如果你需要它)

name = raw_input("What is your name: ") 

def test(username): 
    print "Hi %s, this will be amazing if it works" % (username,) 

test(name) 

根据您的其他意见,这是做到这一点的安全方式:

# 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() 
+0

这是一个很好的答案,但其中的很多部分可能会超过大多数11岁儿童的头。 – Anorov 2013-05-01 23:20:22

+2

试图添加一些评论。 – sberry 2013-05-02 01:34:51

+0

我只是回到这个问题,但你可以使用Python 2.7的input()函数来评估输入。我想这是你所指的“安全方式” – 2013-05-02 02:53:13

0

你如果要使用输入,则必须将raw_input()的值分配给某个变量。

然后请记住,在def(缩进部分)中的所有内容在您调用def之前不会执行。这可能就是为什么没有任何工作,你没有调用def。

只需将test()放在某处并打印即可。

函数在您调用它之前不会运行。调用时执行的代码是'def'下的缩进部分。您可以将代码块放入可能需要多次调用的函数中,而无需每次都重新写入。

+0

确定对不起,因为不清楚家伙,我想要什么 – mrkzam123 2013-05-01 22:13:53

+0

我想要做的就是拥有它,所以如果我在raw_input中输入“test”,它会调用def – mrkzam123 2013-05-01 22:14:53

+0

,所以它会像某些命令一样工作 – mrkzam123 2013-05-01 22:15:45

0
input = raw_input() 

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

if input == "test": 
    test()