请求完善Python货币转换类的course方法与比较运算符
货币转换器实现求助
需求说明
需要实现支持3种货币的转换器,具体要求:
- 实现
Currency基类及Euro、Dollar、Pound子类,汇率规则:1 EUR == 2 USD == 100 GBP - 需实现以下方法:
- course:类方法,返回格式为
"{float value} {currency to} for 1 {currency for}"的汇率字符串 - to_currency:将当前货币转换为目标货币实例
- +运算符:返回新的货币实例,结果为当前货币类型
- 比较运算符:支持
>、<、==
现有代码已完成部分to_currency和__add__,但需要完善course方法与比较运算符,同时原有代码存在逻辑问题(比如to_currency返回数值而非实例、__add__硬编码变量),需一并修复。
现有代码
from __future__ import annotations from typing import Type class Currency: """ 1 EUR = 2 USD = 100 GBP 1 EUR = 2 USD ; 1 EUR = 100 GBP 1 USD = 0.5 EUR ; 1 USD = 50 GBP 1 GBP = 0.02 USD ; 1 GBP = 0.01 EUR """ def __init__(self, value: float): self.value = value @classmethod def course(cls, other_cls: Type[Currency]) -> str: pass def to_currency(self, other_cls: Type[Currency]): pass class Euro(Currency): def __init__(self, value: float): super().__init__(value) self.value = value def to_currency(self, other_cls: Type[Currency]): if other_cls == Dollar: result = float(self.value * 2) return result elif other_cls == Pound: result = float(self.value * 100) return result elif other_cls == Euro: result = float(self.value) return result def __add__(self, other): if other == r: return self.value + 1 elif other == d: return self.value + 50 def __lt__(self, other): if other == r: return False elif other == d: return False def __gt__(self, other): return self.value > other def __eq__(self, other): if other == r: return self.value == r class Dollar(Currency): def __init__(self, value: float): super().__init__(value) self.value = value def to_currency(self, other_cls: Type[Currency]): if other_cls == Dollar: result = float(self.value) return result elif other_cls == Pound: result = float(self.value * 50) return result elif other_cls == Euro: result = float(self.value * 0.5) return result def __add__(self, other): if other == r: return self.value + 2 elif other == e: return self.value + 200 class Pound(Currency): def __init__(self, value: float): super().__init__(value) self.value = value def to_currency(self, other_cls: Type[Currency]): if other_cls == Dollar: result = float(self.value * 0.02) return result elif other_cls == Pound: result = float(self.value) return result elif other_cls == Euro: result = float(self.value * 0.01) return result def __add__(self, other): if other == e: return self.value + 20000 elif other == d: return self.value + 10000
完善后的代码
from __future__ import annotations from typing import Type class Currency: """ 1 EUR = 2 USD = 100 GBP 1 EUR = 2 USD ; 1 EUR = 100 GBP 1 USD = 0.5 EUR ; 1 USD = 50 GBP 1 GBP = 0.02 USD ; 1 GBP = 0.01 EUR """ # 定义货币代码和基准转换系数(以EUR为基准) CODE = "" EUR_RATE = 1.0 def __init__(self, value: float): self.value = value @classmethod def course(cls, other_cls: Type[Currency]) -> str: # 计算1单位当前货币可兑换的目标货币数量 exchange_rate = (cls.EUR_RATE / other_cls.EUR_RATE) return f"{exchange_rate} {other_cls.CODE} for 1 {cls.CODE}" def to_currency(self, other_cls: Type[Currency]): # 先转换为EUR基准,再转换为目标货币 eur_value = self.value * self.EUR_RATE target_value = eur_value / other_cls.EUR_RATE return other_cls(target_value) def _to_eur(self) -> float: # 辅助方法:转换为EUR数值,用于比较和运算 return self.value * self.EUR_RATE def __add__(self, other: Currency) -> Currency: # 转换为当前货币类型返回 other_in_self = other.to_currency(self.__class__).value return self.__class__(self.value + other_in_self) def __lt__(self, other: Currency) -> bool: return self._to_eur() < other._to_eur() def __gt__(self, other: Currency) -> bool: return self._to_eur() > other._to_eur() def __eq__(self, other: Currency) -> bool: return abs(self._to_eur() - other._to_eur()) < 1e-9 # 浮点精度处理 def __repr__(self) -> str: return f"{self.value} {self.CODE}" class Euro(Currency): CODE = "EUR" EUR_RATE = 1.0 # 自身就是基准,系数为1 class Dollar(Currency): CODE = "USD" EUR_RATE = 0.5 # 1 USD = 0.5 EUR class Pound(Currency): CODE = "GBP" EUR_RATE = 0.01 # 1 GBP = 0.01 EUR
关键修复与说明
course方法:
- 通过基类定义的
EUR_RATE基准系数,统一计算汇率,避免子类重复写逻辑 - 利用
CODE属性获取货币标识,直接生成要求格式的字符串
- 通过基类定义的
比较运算符:
- 新增
_to_eur辅助方法,将所有货币转换为EUR基准值进行比较,保证逻辑统一 - 处理浮点精度问题,
__eq__方法使用误差范围判断相等
- 新增
修复原有代码问题:
to_currency现在返回目标货币实例而非数值,符合需求示例__add__方法不再硬编码变量,而是通过to_currency将另一种货币转换为当前类型后相加,返回当前类型的实例- 新增
__repr__方法,实现示例中的打印格式
测试验证
运行以下代码可验证所有功能:
# 测试course方法 print( f"Euro.course(Pound) ==> {Euro.course(Pound)}\n" f"Dollar.course(Pound) ==> {Dollar.course(Pound)}\n" f"Pound.course(Euro) ==> {Pound.course(Euro)}\n" ) # 测试to_currency方法 e = Euro(100) r = Pound(100) d = Dollar(200) print( f"e = {e}\n" f"e.to_currency(Dollar) = {e.to_currency(Dollar)}\n" f"e.to_currency(Pound) = {e.to_currency(Pound)}\n" f"e.to_currency(Euro) = {e.to_currency(Euro)}\n" ) print( f"r = {r}\n" f"r.to_currency(Dollar) = {r.to_currency(Dollar)}\n" f"r.to_currency(Euro) = {r.to_currency(Euro)}\n" f"r.to_currency(Pound) = {r.to_currency(Pound)}\n" ) # 测试+运算符 print( f"e + r => {e + r}\n" f"r + d => {r + d}\n" f"d + e => {d + e}\n" ) # 测试比较运算符 print(f"e > d: {e > d}") # False(100 EUR = 200 USD) print(f"r < e: {r < e}") # True(100 GBP = 1 EUR < 100 EUR) print(f"d == e: {d == e}") # True
内容的提问来源于stack exchange,提问作者user24032725
相关产品推荐
相关产品推荐

