简体   繁体   English

如何创建将分数分成最简单形式的函数python

[英]How do you make a function that divides fractions into simplest form python

I am taking a class and i'm confused. 我正在上课,我很困惑。 It would really help if you could guide me through the proccess of this and tell me what I am doing wrong. 如果您能指导我完成此过程并告诉我我做错了什么,那将真的有帮助。 I have an error that has to do with the parentheses since theres nothing in them. 我有一个与括号有关的错误,因为括号中没有任何内容。 I am a newbie so i'm sorry. 我是新手,对不起。

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

Your function is taking in arguments a , b , c , and d , but you're not using them anywhere. 您的函数接受参数abcd ,但是您没有在任何地方使用它们。 You're instead defining four new variables. 您要定义四个新变量。 Try: 尝试:

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

and get rid of your empty parentheses bits, see if that does what you are trying to do. 并删除空括号位,看看是否可以完成您要尝试的操作。

you cannot declare a variable as you are doing n = () and then try to assign an integer or string to it. 您无法在执行n =()时声明变量,然后尝试为其分配整数或字符串。

n=() does not mean: n =()并不意味着:

n equals nothing at the moment but i will assign a variable shortly. n目前不等于任何值,但我不久将分配一个变量。

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

They are two examples of sequence data types (see Sequence Types — list, tuple, range). 它们是序列数据类型的两个示例(请参见序列类型-列表,元组,范围)。 Since Python is an evolving language, other sequence data types may be added. 由于Python是一种不断发展的语言,因此可以添加其他序列数据类型。 There is also another standard sequence data type: the tuple. 还有另一种标准序列数据类型:元组。

so within your function, if you want you varialbes to be assigned what is passed as an argument 因此在函数中,如果您希望为varialbes分配作为参数传递的内容

for Ex: 例如:

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

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

consider reading more on tuples from the above link 考虑从上述链接中阅读有关元组的更多信息

n=() is a valid python statement and there is no issue with that. n=()是有效的python语句,因此没有问题。 However n=() is evaluating n to an empty tuple() . 但是n=()n评估为一个空的tuple() I believe that what you are trying to do is as follows. 我相信您想要做的如下。

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

here is a simpiler way of writing the same function 这是编写相同功能的简单方法

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

print FractionDivider_2(1,2,3,4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM