鱼C论坛

 找回密码
 立即注册
查看: 1600|回复: 7

[已解决]布尔问题

[复制链接]
发表于 2020-11-6 21:42:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 qin_yin 于 2020-11-6 21:46 编辑

import time as t
'''time.localtime([secs])
接收时间辍(1970 纪元年后经过的浮点秒数)并返回当地时间下的时间元组 t(t.tm_isdst 可取 0 或 1,取决于当地当时是不是夏令时)
'''
class timer:
    beg = 0
    end = 0
    #开始计时
    def start(self):
        self.beg = t.localtime()
        print('开始计时')
    #停止计时
    def stop(self):
        if not self.beg:
            print('提示:请先启动计时')
        else:
            self.end = t.localtime()
            self._count_time()

    #总计时间
    def _count_time(self):
        total_s = ''
        for i in range(6):
            temp = str(self.end - self.beg)
            print(bool(temp))
            if temp:
                total_s += temp
        print(total_s)
        self.beg = 0
        self.end = 0

a = timer()
a.start()
t.sleep(3)
a.stop()

以上模块并未完善,在中途发现了个问题
temp = str(self.end - self.beg),当这里的结果为0,0不是相当于False,为什么这里0属于True
最佳答案
2020-11-6 22:30:32
虽然不是很明白你这代码要计算什么,猜你要总计循环6次的时间?
  1. import time as t
  2. '''time.localtime([secs])
  3. 接收时间辍(1970 纪元年后经过的浮点秒数)并返回当地时间下的时间元组 t(t.tm_isdst 可取 0 或 1,取决于当地当时是不是夏令时)
  4. '''
  5. class timer:
  6.     beg = 0
  7.     end = 0
  8.     #开始计时
  9.     def start(self):
  10.         self.beg = t.time()
  11.         print('开始',self.beg)
  12.         print('开始计时')
  13.     #停止计时
  14.     def stop(self):
  15.         if not self.beg:
  16.             print('提示:请先启动计时')
  17.         else:
  18.             self.end = t.time()
  19.             print('结束',self.end)
  20.             self._count_time()

  21.     #总计时间
  22.     def _count_time(self):
  23.         total_s = 0
  24.         for i in range(6):
  25.             temp = self.end - self.beg
  26.             print('间隔',temp)
  27.             print(bool(str(temp)))
  28.             if temp:
  29.                 total_s += temp
  30.         print('总计',total_s)
  31.         self.beg = 0
  32.         self.end = 0

  33. a = timer()
  34. a.start()
  35. t.sleep(3)
  36. a.stop()
复制代码


然后你说的布尔问题
  1. print(bool(str(0)))
  2. print(bool(0))
复制代码

结果是:
True
False
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-6 21:46:01 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-6 21:47:06 | 显示全部楼层

刚才没编辑好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-6 21:51:16 | 显示全部楼层
而且这里 if temp:
                total_s += temp
写成:if temp != 0:当temp为0的时候依然可以进入这个条件,亲测
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-6 22:19:10 | 显示全部楼层
qin_yin 发表于 2020-11-6 21:51
而且这里 if temp:
                total_s += temp
写成:if temp != 0:当temp为0的时候依然可以进入这 ...
  1.     #总计时间
  2.     def _count_time(self):
  3.         total_s = ''
  4.         for i in range(6):
  5.             temp = str(self.end - self.beg)
  6.             print(bool(temp))
  7.             if temp:
  8.                 total_s += temp
  9.         print(total_s)
  10.         self.beg = 0
  11.         self.end = 0
复制代码


能解释下你这个for 0到5是什么用意吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-6 22:30:32 | 显示全部楼层    本楼为最佳答案   
虽然不是很明白你这代码要计算什么,猜你要总计循环6次的时间?
  1. import time as t
  2. '''time.localtime([secs])
  3. 接收时间辍(1970 纪元年后经过的浮点秒数)并返回当地时间下的时间元组 t(t.tm_isdst 可取 0 或 1,取决于当地当时是不是夏令时)
  4. '''
  5. class timer:
  6.     beg = 0
  7.     end = 0
  8.     #开始计时
  9.     def start(self):
  10.         self.beg = t.time()
  11.         print('开始',self.beg)
  12.         print('开始计时')
  13.     #停止计时
  14.     def stop(self):
  15.         if not self.beg:
  16.             print('提示:请先启动计时')
  17.         else:
  18.             self.end = t.time()
  19.             print('结束',self.end)
  20.             self._count_time()

  21.     #总计时间
  22.     def _count_time(self):
  23.         total_s = 0
  24.         for i in range(6):
  25.             temp = self.end - self.beg
  26.             print('间隔',temp)
  27.             print(bool(str(temp)))
  28.             if temp:
  29.                 total_s += temp
  30.         print('总计',total_s)
  31.         self.beg = 0
  32.         self.end = 0

  33. a = timer()
  34. a.start()
  35. t.sleep(3)
  36. a.stop()
复制代码


然后你说的布尔问题
  1. print(bool(str(0)))
  2. print(bool(0))
复制代码

结果是:
True
False
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-6 22:34:07 | 显示全部楼层
  1.         for i in range(6):
  2.             temp = self.end - self.beg
  3.             print(bool(temp))
  4.             if temp:
  5.                 total_s += str(temp)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-6 22:50:15 | 显示全部楼层
昨非 发表于 2020-11-6 22:19
能解释下你这个for 0到5是什么用意吗

这是还没写完的代码,我是想for循环t.localtime()返回的元组,相减得到用时长,年月日
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 13:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表