《python语言程序设计》2018版第8章20题使用Rational类编写一个程序(上)-修改一下8-4Rational类我认为的错误

在这里插入图片描述
首先抄一下Rational类,可以安静的抄一遍

书中214-215页间程序清单8-4的代码如下:


class Rational:
    def __init__(self, numerator=1, denominator=0):
        divisor = gcd(numerator, denominator)
        self.__numerator = (1 if denominator > 0 else -1) * int(numerator / divisor)
        self.__denominator = int(abs(denominator) / divisor)

    def __add__(self, second_rational):
        n = self.__numerator * second_rational[1] + self.__denominator * second_rational[0]
        d = self.__denominator * second_rational[1]
        return Rational(n, d)

    def __sub__(self, second_rational):
        n = self.__numerator * second_rational[1] - self.__denominator * second_rational[0]
        d = self.__denominator * second_rational * second_rational[1]
        return Rational(n, d)

    def __mul__(self, second_rational):
        n = self.__numerator * second_rational[0]
        d = self.__denominator * second_rational[1]
        return Rational(n, d)

    def __truediv__(self, second_rational):
        n = self.__numerator * second_rational[1]
        d = self.__denominator * second_rational[0]
        return Rational(n, d)

    def __float__(self):
        return self.__numerator / self.__denominator

    def __int__(self):
        return int(self.__float__())

    def __str__(self):
        if self.__denominator == 1:
            return str(self.__numerator)
        else:
            return str(self.__numerator) + "/", (self.__denominator)

        def __lt__(self, second_rational):
            return self.__cmp__(second_rational) < 0

        def __le__(self, second_rational):
            return self.__cmp__(second_rational) <= 0

        def __gt__(self, second_rational):
            return self.__cmp__(second_rational) > 0

        def __ge__(self, second_rational):
            return self.__cmp__(second_rational) >= 0

        def __cmp__(self, second_rational):
            temp = self.__sub__(second_rational)
            if temp[0] > 0:
                return 1
            elif temp[0] < 0:
                return -1
            else:
                return 0

        def __getitem__(self, index):
            if index == 0:
                return self.__numerator
            else:
                return self.__denominator

        def gcd(n, d):
            n1 = abs(n)
            n2 = abs(d)
            gcd = 1

            k = 1
            while k <= n1 and k <= n2:
                if n1 % k == 0 and n2 % k == 0:
                    gcd = k
                k += 1
            return gcd

这段代码抄的我问题重重.

各种报错的截图1

在这里插入图片描述
比如这个为什么断开了少了一个括号呀.一个个连锁反应

各种报错的截图2

请添加图片描述

各种报错的截图3

请添加图片描述

各种报错的截图4

请添加图片描述

添加一个str
 def __str__(self):
        if self.__denominator == 1:
            return str(self.__numerator)
        else:
            #str(self.__denominator) 成功
            return str(self.__numerator) + "/", str(self.__denominator)
我将n和d修改为self
    def gcd(self):
        n1 = abs(self.__numerator)
        n2 = abs(self.__denominator)
        gcd = 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电饭叔

谢谢各位兄弟们的关注,谢谢破费

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值