python - 类型错误:+ 不支持的操作数类型:“浮点”和“时间”
问题描述
我试图让它工作,似乎问题是当我尝试添加一个小时和一个小数表示时间(例如 .75 等于 45 分钟)。我想我需要在init () 中添加一些东西,但是我不确定什么会解决它。浮标?
class Time():
def __init__(self,a=0.0,b=0.0):
self.hour = a
self.minute = b
self.hour_dec = a-int(a)
self.total_hour = self.hour - self.hour_dec
self.extraMin = self.hour_dec * 60
self.total_min = self.minute + self.extraMin
if self.total_min > 60:
self.total_min= self.total_min - 60
self.total_hour += 1
def __str__(self):
return str(self.total_hour)+" hours, "+str(self.total_min)+" minutes "
def __add__(self, other): #Brad suggested the if statement to get this part to work
if isinstance(other,int):
newHour = self.total_hour + other
newMinute = self.total_min
return Time(newHour,newMinute)
else:
newHour = self.total_hour + other.total_hour
newMinute = self.total_min + other.total_min
return Time(newHour,newMinute)
def main():
time0= Time()
print(time0)
time1 = Time(6)
print(time1)
time2 = Time(3,30)
print(time2)
time3 = Time(30,75)
print(time3)
time4 = Time(3.5)
print(time4)
print(time3 + time4)
print(time3)
print(time1 + 10)
print(.75 + time2)
main()
解决方案
推荐阅读
- sqlite - 在 Electron/TypeORM 中找不到 SQLite 包
- excel - 如何在行的副本中创建例外
- spring - 如何使用 Spring JPA 但不使用实体创建存储过程
- raspberry-pi - gRPC 不适用于树莓派 (armv7l)
- c# - 将 XML 发布到 webapi 只是在 [FromBody] 请求模型上绑定非集合属性
- python - 防止 pyttsx3 冻结 GUI
- jboss - infinispan 钝化可以与基于内存的驱逐一起使用吗?
- neural-network - 如何训练流水线神经网络?
- python - 如何从存储在 .csv 中的 pandas 数据框中读取 np 矩阵?
- javascript - 如何将原型 jquery 函数转换为 javascript 原型函数