2020-06-11 python解题 分享

 

 

今天的特殊题

下图中的4.30题 我在第一次做的时候已经得出结果。我当时的判定是当前时间+时区-12然后小于12的就按AM显示。这个题之前的结果输入北京时间是成功的,我并没有太多异议,今天做题的开始我想换一个思路于是按图索骥去找到第2章的2.18题。并且根据他找到了2-7的程序。(程序中带下划线的都是原2-7的内容)然后我半抄半写的编辑,结果出现了错误。而且是非常多的错。这个题是让我们以12小时的方式显示。那么下午2点。应该是2点pm,但是我这里出现了14点pm。

 我当时重看了自己写的旧题。没有错。就继续试,试了多次发现原来是我第一次写的时候其实并不是很完美出现了错误造成的,于是我开始重新思考。

    不单纯是大于12来判定。还有显示的问题。显示的基本都是15点或者18点。并不是12小时,于是我想到用除余。“%”这个功能。也就是在我这里的curTime即当前小时和时区相加大于12的时候,对curTime执行,curTime % 12的条件,这样他的显示就正确了。

    对比自己过去的解题方法,再重新写程序确实能发现自己很多很多问题。

    谢谢大家的观看

 

 

 

 

 

 

Python程序

 

import time

currentTime = time.time()

totalSeconds = int(currentTime)

currentSecond = totalSeconds % 60

totalMinutes = totalSeconds // 60

currentMinute = totalMinutes % 60

totalHours = totalMinutes // 60

currentHour = totalHours % 24
print(f"currentHour is {currentHour}")
zoneTime = eval(input("Enter the time zone offset to GMT: "))
curTime = abs(currentHour + zoneTime)
print(curTime)
if curTime > 12:
    print("Current time is", curTime % 12, ":",
          currentMinute, ":", currentSecond, "PM")
else:
    print("Current time is", abs(curTime), ":",
          currentMinute, ":", currentSecond, "AM")

 

posted @ 2020-06-11 10:42  yogaPandas  阅读(96)  评论(0编辑  收藏  举报