4

私はpythonが初めてで、raw_inputと関数を使用してプログラムのコマンドのようなものを作ろうとしています。何らかの理由で機能していません。これが私がテストしてきたコードです:

raw_input()

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

4 に答える 4

6

raw_input何かを入力するまでブロックします。改行を受け取ると (ユーザーが Enter キーを押します)、値が返され、保存できます。関数を呼び出そうとしているようには見えません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()
于 2013-05-01T22:06:45.947 に答える
3

raw_input() の出力を次のように割り当てる必要があります(ドキュメント):

s = raw_input('--> ')

また、コードは機能します (驚くべきことですよね?) 関数を定義しただけで、それを呼び出していません。これを Python ファイルの末尾に追加します (インデントなしで左端まで):

test()
于 2013-05-01T22:05:05.097 に答える
0

入力を使用する場合は、raw_input() の値を変数などに割り当てる必要があります。

次に、def を呼び出すまで、def 内のすべて (インデントされた部分) が実行されないことを覚えておいてください。それがおそらく何も機能しない理由です。あなたはdefを呼び出していません。

test() をどこかに置いて呼び出すだけで、印刷されます。

関数は、呼び出すまで実行されません。呼び出されたときに実行されるコードは、「def」の下のインデントされた部分です。何度も呼び出す可能性のある関数にコードを入れることができ、そのたびにコードを書き直す必要がありません。

于 2013-05-01T22:10:03.207 に答える
0
input = raw_input()

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

if input == "test":
    test()
于 2013-05-02T03:13:16.847 に答える