2013-05-01 2 views

답변

3

당신은 너무처럼 뭔가에 raw_input의 출력을() 할당 (documentation)가 필요합니다 : : 여기에 테스트했던 코드는

또한
s = raw_input('--> ') 

코드는 놀라운 일을 (수행 맞습니까?) 방금 함수를 정의했지만 호출하지 않았습니다. (모든 방법을 왼쪽으로, 아니 들여 쓰기) 파이썬 파일의 끝에이 추가 : 당신이 뭔가를 입력 할 때까지

test() 
+0

함수는 어떻게 작동합니까? 내 말은, raw_input이 지정 될 때까지 아무 일도하지 않기 위해서이다. – mrkzam123

+0

함수 정의 및 해당 함수 호출은 별도의 단계입니다. @ sberry의 대답은이 모든 것을 하나의 스크립트로 함께 보여줍니다. –

6

raw_input이 차단됩니다 개행 문자가 수신되면 (사용자가 Enter 키를 누를) 값을. 반환되며 저장 될 수 있습니다. 귀하의 기능을 호출하려고 시도한 적이 나타나지 않습니다. test.

# 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

+2

몇 가지 설명을 추가하려고 시도했습니다. – sberry

+0

나는이 질문으로 돌아가지만, Python 2.7의 input() 함수를 사용하여 입력을 평가할 수있다. 나는 당신이 "안전한 방법"으로 암시 한 것이라고 생각합니다. –

0

당신을 : 아마 당신은이 같은 (당신이 그것을 필요로하는 경우에 나는 더 설명 할 수)

다른 의견을 바탕으로
name = raw_input("What is your name: ") 

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

test(name) 

는이이 작업을 수행 할 수있는 안전 방법을 시도하려는 입력을 사용하려면 raw_input() 값을 변수와 같은 값으로 지정해야합니다.

그런 다음 def (들여 쓰는 부분) 내부의 모든 내용은 def를 호출 할 때까지 실행되지 않습니다. 아마 아무 것도 작동하지 않는 것입니다. 당신은 def를 부르지 않을 것입니다.

그냥 test()를 어딘가에 두어 호출하면 인쇄됩니다.

함수를 호출 할 때까지 함수가 실행되지 않습니다. 호출 될 때 실행되는 코드는 'def'아래의 들여 쓰기 부분입니다. 매번 다시 작성할 필요없이 여러 번 호출 할 수있는 함수에 코드 조각을 넣을 수 있습니다.

+0

ok 불투명 한 사람들에게 유감스럽게 생각합니다. 내가 원하는 것은 – mrkzam123

+0

입니다. raw_input에 "test"라고 입력하면 – mrkzam123

+0

을 호출하므로 어떤 종류의 명령으로 작동합니다. – mrkzam123

0
input = raw_input() 

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

if input == "test": 
    test() 
관련 문제