程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] learning notes week7-0 time conversion

編輯:Python

Selection structure 8- Time shift 3( Increase the number of days )- Single branch

Title Description

Convert seconds to time format (24 hourly , If the number of hours is greater than 23, Convert to days )

Input

Enter seconds n(n>0 The integer of )

Output

Convert seconds into time format (24 hourly , If the number of hours is greater than 23, Convert to days , If the number of hours is less than 24, The number of days will not be output ) Such as input :3612 , Output is 01:00:12 Such as input :75612 , Output is 21:00:12 Such as input :162012 , Output is 1 day 21:00:12

The sample input

162012

Sample output

1 day 21:00:12

Tips

The output format is %02d

a=eval(input())
if a>86400:
d=a//86400
h=a%86400//3600
m=a//60%60
s=a%60
print("{} day {:0>2}:{:0>2}:{:0>2}".format(d,h,m,s))
else:
h=a//3600
m=a//60%60
s=a%60
print("{:0>2}:{:0>2}:{:0>2}".format(h,m,s))

Selection structure 9- Time shift 4(AM+PM)- Double branch

Title Description

Convert seconds to time format (12 hourly )

Input

Enter seconds n(n<86400( Seconds in a day )

Output

Convert seconds into time format (12 hourly ) Such as input :3612 , Output is AM 01:00:12 Such as input :75612 , Output is PM 09:00:12

Be careful AM For the range of :0 second ~43199 second (11:59:59) by AM,(43200 second ~86399 second ) by PM

The sample input

3612

Sample output

AM 01:00:12

Tips

Time display format method , how 1:0:12   Is shown as 01:00:12  Format

With the formatter printf("%02d:%02d:%02d",h,m,s);

a=eval(input())
if a>43199:
h=a%43200//3600
m=a//60%60
s=a%60
print("PM {:0>2}:{:0>2}:{:0>2}".format(h,m,s))
else:
h=a//3600
m=a//60%60
s=a%60
print("AM {:0>2}:{:0>2}:{:0>2}".format(h,m,s))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved