저는 python에 익숙하지 않아 raw_input 및 함수를 사용하여 프로그램에 명령을 적용하려고합니다. 웬일인지 그것은 일하지 않고 있었다.함수와 함께 원시 입력을 사용하려고 시도했습니다.
raw_input()
def test():
print "hi, this will be amazing if it works"
저는 python에 익숙하지 않아 raw_input 및 함수를 사용하여 프로그램에 명령을 적용하려고합니다. 웬일인지 그것은 일하지 않고 있었다.함수와 함께 원시 입력을 사용하려고 시도했습니다.
raw_input()
def test():
print "hi, this will be amazing if it works"
당신은 너무처럼 뭔가에 raw_input의 출력을() 할당 (documentation)가 필요합니다 : : 여기에 테스트했던 코드는
또한s = raw_input('--> ')
코드는 놀라운 일을 (수행 맞습니까?) 방금 함수를 정의했지만 호출하지 않았습니다. (모든 방법을 왼쪽으로, 아니 들여 쓰기) 파이썬 파일의 끝에이 추가 : 당신이 뭔가를 입력 할 때까지
test()
함수는 어떻게 작동합니까? 내 말은, raw_input이 지정 될 때까지 아무 일도하지 않기 위해서이다. – mrkzam123
함수 정의 및 해당 함수 호출은 별도의 단계입니다. @ sberry의 대답은이 모든 것을 하나의 스크립트로 함께 보여줍니다. –
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()
당신을 : 아마 당신은이 같은 (당신이 그것을 필요로하는 경우에 나는 더 설명 할 수)
다른 의견을 바탕으로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'아래의 들여 쓰기 부분입니다. 매번 다시 작성할 필요없이 여러 번 호출 할 수있는 함수에 코드 조각을 넣을 수 있습니다.
input = raw_input()
def test():
print "hi, this will be amazing if it works"
if input == "test":
test()
그리고 무엇을하려고합니까? – fgb