2016-06-07 25 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