2014-09-25 93 views
0
# any help is greatly appreciated new to this stuff 

def total_bases(int1,int2,int3,int4): 
    """(int, int, int, int) -> integer 
    Return the total number of bases covered 
    >>>total_bases(2,3,4,5) 
    40 
    """ 

    return int1+2*int2+3*int3+4*int4 

def slugging_percentage(total_bases,int5): 
    """ (total_bases, int5) -> float # so for instance i need the product of the first function for the second function 
    Return the slugging percentage given the total number of bases covered and the total number at bat(int5) 
    >>>slugging_percentage(20,9) 
    2.22 
    """ 

    return total_bases/int5 

def on_base_percentage(h,bb,hbp,ab,sf): 
    """(int,int,int,int,int) -> float 
    Return the on-base percentage given the hits, base on balls, hit by pitch, at bats and sacrfice hits 
    >>>on_base_percentage(1,2,3,4,5) 
    0.43 
    """ 

    return (h+bb+hbp)/(ab+bb+hbp+sf) 

def on_base_plus_slugging(on_base_percentage,slugging_percentage): 
    """(float,float) -> float # as well as for this 
    Return the on-base plus slugging given the on-base percentage and the slugging percentage 
    >>>on_base_plus_slugging(1.0,2.0) 
    3.0 
    """ 

    return on_base_percentage+slugging_percentage 

def OPS_value(on_base_plus_slugging): 
    """(float) -> string 
    Return the on-base plus slugging value given the on-base plus slugging score range 
    >>>OPS_value(0.8234) 
    B 
    """ 
if on_base_plus_slugging > 0.9000: 
    return "A" 
elif on_base_plus_slugging > 0.7667: 
    return "B" 
elif on_base_plus_slugging > 0.7000: 
    return "C" 
else on_base_plus_slugging < 0.7000: 
    return "F" 
elif on_base_plus_slugging == 0.7000: 
    return "F" 
+0

請格式化此代碼。 – sheeptest 2014-09-25 23:44:33

+0

那麼問題是什麼? – wookie919 2014-09-25 23:50:08

+0

@sheeptest,我認爲最好是省略代碼,因爲它不是OP詢問問題的代碼。他問的是在函數內部調用一個函數,對嗎? – 2014-09-25 23:50:10

回答

-1

將全局變量保存爲一種方法。

def some_function(): 
     global var  #This is a global variable 
     #Do things to variable 

另一種方式,我相信你正在尋找的是調用一個函數內的函數。這應該是這樣的:

def function_1(#some variable): 
     #Stuff could be up here 
     num = function2(5) 
     #Stuff could be down here 

def function_2(a_number): 
     a_number = a_number*2 
     return a_number 

這將使變量num = 5 * 2。

我希望這會有所幫助。

+0

由sheeptest提出的解決方案比在另一箇中使用函數要乾淨得多。定義清晰輸入和輸出的函數是更復雜的代碼更好的構建塊。它們可以在不同的環境中重複使用,並且更容易測試。當然,在另一個函數內調用一個函數是一個完全有效的方法,並且仍然可以工作。只是一個風格問題。 – pawel 2017-09-25 02:36:18

1

函數可以返回值。您可以將這些值存儲爲變量。然後你可以使用這些值作爲其他函數的輸入。

我想你試圖用on_baseslugging百分比來計算OPS_value

因此,您將計算on_base,total_basesslugging並將返回的值存儲在變量中。

然後您將這些變量作爲輸入傳遞給您的OPS_value函數,該函數返回最終的計算值。

見下面的例子:

def OPS_value(percent): 
    """(float) -> string 
    Return the on-base plus slugging value given the on-base plus slugging score range 
    >>>OPS_value(0.8234) 
    B 
    """ 

    if percent > 0.9000: 
     return "A" 
    elif percent > 0.7667: 
     return "B" 
    elif percent > 0.7000: 
     return "C" 
    else: 
     return "F" 

total_bases = total_bases(2, 3, 4, 5) # Get the return value for total_bases 

slugging = slugging_percentage(total_bases, 9) # Get the return value for slugging_percent 
on_base = on_base_percentage(1, 2, 3, 4, 5) 
print OPS_value(on_base + slugging) # using on_base + slugging as input 

我們正在試圖做的是保持與計算每一件事情total_basesslugging等分開算算。

你原來的代碼的另一個主要變化是你不需要有一個函數來添加兩個值。 你可以也應該在一行中做到這一點。

相關問題