2016-06-07 79 views
0

我正在上課,我很困惑。如果你能指導我完成這個過程並告訴我我做錯了什麼,那將會非常有幫助。我有一個與括號有關的錯誤,因爲它們中沒有任何內容。我是新手,所以我很抱歉。你怎麼做一個功能,將分數分成最簡單的形式python

def FractionDivider(a,b,c,d): 
    n =() 
    d =() 
    n2 =() 
    d2 =() 
    print int(float(n)/d), int(float(n2)/d2) 
    return float (n)/d/(n2)/d2 
+0

請更新你所得到的錯誤後的simpiler方式。 – AKS

回答

0

您的功能正在採取論證abcd,但你不使用它們的任何地方。而是定義四個新變量。嘗試:

def FractionDivider(n, d, n2, d2): 

並擺脫你的空括號位,看看你是否做了你想做的事。

0

你不能像你在做n =()那樣聲明一個變量,然後嘗試給它分配一個整數或字符串。

N =()並不意味着:

n等於什麼的時刻,但我很快就會分配一個變量。

()--->元組https://docs.python.org/3/tutorial/datastructures.html

它們是序列數據類型的兩個實例(見序列類型 - 列表,元組,範圍)。由於Python是一種不斷髮展的語言,因此可能會添加其他 序列數據類型。另外還有一個標準的 序列數據類型:元組。

所以你的函數中,如果你想你varialbes什麼作爲參數

防爆傳遞給被分配:

def FractionDivider(a,b,c,d): 

    n = a 
    d = b 
    n2 = c 
    d2 = d 

考慮從上面的鏈接閱讀更多的元組

0

n=()是一個有效的Python語句,並沒有問題。然而n=()正在評估n到一個空的tuple()。我相信你所要做的是如下。

def FractionDivider(a,b,c,d): 
    ''' 
     Divides a fraction by another fraction... 
     ''' 

    n = a #setting each individual parameter to a new name. 
    d = b #creating a pointer is often useful in order to preserve original data 
    n2 = C#but it is however not necessary in this function 
    d2 = d 
    return (float(n)/d)/(float(n2)/d2) #we return our math, Also order of operations exists here '''1/2/3/4 != (1/2)/(3/4)''' 

print FractionDivider(1, 2, 3, 4) #here we print the result of our function call. 

#indentation is extremely important in Python 

這裏是寫同樣的功能

def FractionDivider_2(n,d,n2,d2): 
    return (float(n)/d)/(float(n2)/d2) 

print FractionDivider_2(1,2,3,4) 
+0

感謝您的幫助!我放下你放的東西,但我仍然沒有得到正確的答案。 – zbush548

+0

第一個函數的代碼有一個錯誤......'d2 = d'只是將'd2'賦值給參數'b'。還要注意Python會用'/'運算符進行浮點除法,所以你不需要用'float()'來投射東西。 – Riaz

+0

關於命名您是正確的,我試圖遵守OP的命名約定。至於浮點除法,pythons /運算符將執行浮點除法,但取決於您的操作系統和python版本,如果其中一個值不是float類型,它將返回int版本。 – TheLazyScripter