2016-06-11 4 views
-3

このプログラムの機能部分(def以降のすべて)は機能しません。エラーメッセージはありません。私はZedのやり方と同じようにすべてをタイプしたと信じています。どうしましたか?python難しい方法を学ぶex 24関数が動作しない、エラーメッセージなし

print "Let's practice everything." 
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tell' 
# newline and a space are put in, tab and a space are put in 

# this doesn't print the poem, only stores it 
poem = """ 
\tThe lovely world 
with logic so firmly planted 
cannot discern \n the needs of love 
nor comprehend passion from intuition 
and requires an explanation 
\n\t\t where there is none 
""" 

print "--------------" 
print poem 
print "--------------" 

five = 10 - 2 + 3 -6 
print "This should be five: %s" % five 

def secret_formula(started): 
    jelly_beans = started * 500 
    jars = jelly_beans/1000 
    crates = jars/100 
    return jelly_beans, jars, crates 
    # these are local variables, they are inside the function 

    start_point = 10000 
    # whatever happened to started now happens to start point 
    # these are global variables, they are outside the function, could be called x, y, and z 
    beans, jars, crates = secret_formula(start_point) 

    print "With a starting point of: %d" % start_point 
    print "We'd have %d beans, %d jars, and %d crates." (beans, jars, crates) 

    # reassigns with new value based on old value 
    start_point = start_point/10 

    print "We can also do that this way:" 
    # brings down the info from line 29 
    print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point) 

答えて

2

あなたのコードは決して呼び出し機能。そしてそれは、をあまりにも遠くにインデントしたコードをに入れたからです。

あなたの機能はこれだけのように定義する必要があります。これらの行は、インデントされないべきで後

def secret_formula(started): 
    jelly_beans = started * 500 
    jars = jelly_beans/1000 
    crates = jars/100 
    return jelly_beans, jars, crates 
    # these are local variables, they are inside the function 

すべて

start_point = 10000 
# whatever happened to started now happens to start point 
# these are global variables, they are outside the function, could be called x, y, and z 
beans, jars, crates = secret_formula(start_point) 

print "With a starting point of: %d" % start_point 
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates) 

# reassigns with new value based on old value 
start_point = start_point/10 

print "We can also do that this way:" 
# brings down the info from line 29 
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point) 

注このコードは、すべての先頭から開始する必要があることラインの開始点と各ラインの命令との間にはスペースを入れないでください。

また、これらの行のうちの1つのタイプミスを修正しました。あなたは、入力された:これは、文字列を呼び出そうとし

print "We'd have %d beans, %d jars, and %d crates." (beans, jars, crates) 

(...)"..."文字列リテラルの直後に来ます)。その行に%演算子がありません。書式設定作業が正しく機能するようにしてください。

print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates) 
+0

「secret_formula(5)」と入力しても何も起こりません。エラーメッセージも表示されません。私は別のstackoverflowの質問からこのコードの書き直しを使用してみましたが、それは私にインデントに関するエラーメッセージを与えただけです。 DEF変更(start_point): \t start_point [0] = 3 \t start_point = [1] \t変化(start_point) プリントstart_point http://stackoverflow.com/questions/15148496/python-passing-an-integer - By-reference –

+0

@ChrisTrachte:その投稿はあなたの質問とは関係ありません。あなたは本当にそこに行く必要はありません。 *あなたが投稿したコードは、主に私が指摘した問題を除いて動作します。 –

+0

さて、それを得ました。それは、関数の一部ではないコードのインデントを削除した後と、フォーマット演算子%を追加した後に機能しました。 –

関連する問題