python中24小时制转换为12小时制的方法
作者:C.L.Z@ 时间:2023-01-26 20:50:12
12-24小时制
编写一个程序,要求用户输入24小时制的时间,然后显示12小时制的时间。
输入格式:
输入在一行中给出带有中间的:符号(半角的冒号)的24小时制的时间,如12:34表示12点34分。当小时或分钟数小于10时,均没有前导的零,如5:6表示5点零6分。
提示:在scanf的格式字符串中加入:,让scanf来处理这个冒号。
输出格式:
在一行中输出这个时间对应的12小时制的时间,数字部分格式与输入的相同,然后跟上空格,再跟上表示上午的字符串AM或表示下午的字符串PM。如5:6 PM表示下午5点零6分。注意,在英文的习惯中,中午12点被认为是下午,所以24小时制的12:00就是12小时制的12:0 PM;而0点被认为是第二天的时间,所以是0:0 AM。
输入样例:
21:11
输出样例:
9:11 PM
思路
通过题目分析,可将本题分为判断时钟小于12,大于12和等于12来探讨24小时制转换为12小时制时的结果。
注意
此题要注意24小时制时12点到1点内转换为12小时制的部分,需要单独来判断一下
代码
hour,minute = input().split(':')
hour = int(hour)
minute = int(minute)
if hour < 12:
print('%d:%d AM'%(hour,minute))
elif hour == 12:
print('%d:%d PM'%(hour,minute))
else:
hour = hour-12
print('%d:%d PM'%(hour,minute))
来源:https://blog.csdn.net/weixin_46178942/article/details/104041952
标签:python,24小时制,12小时制
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Python Matplotlib解决绘图X轴值不按数组排序问题
2022-12-03 09:33:06
![](https://img.aspxhome.com/file/2023/0/110050_0s.png)
Python递归函数特点及原理解析
2023-12-02 19:40:40
![](https://img.aspxhome.com/file/2023/8/100898_0s.png)
小议javascript设计模式
2009-10-09 13:31:00
Python实现12306自动抢火车票功能
2022-11-19 04:02:38
![](https://img.aspxhome.com/file/2023/6/90426_0s.jpg)
python爬虫中多线程的使用详解
2021-01-18 09:16:15
SaaS中的用户体验设计
2009-05-20 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20095/20/p2009520103356-74s.jpg)
MSSQL Server 查询优化方法 整理
2024-01-18 02:54:09
基于Vue.js的表格分页组件
2024-05-02 16:41:50
Python带动态参数功能的sqlite工具类
2022-10-02 14:05:15
Python 爬虫学习笔记之正则表达式
2021-07-02 01:50:55
![](https://img.aspxhome.com/file/2023/7/108037_0s.png)
Python决策树和随机森林算法实例详解
2021-12-15 21:44:24
![](https://img.aspxhome.com/file/2023/9/77919_0s.png)
SOSO地图API使用(一)在地图上画圆实现思路与代码
2024-04-10 13:54:07
![](https://img.aspxhome.com/file/2023/8/136948_0s.png)
HTML 5 Video概述
2010-06-23 18:56:00
![](https://img.aspxhome.com/file/UploadPic/20106/23/01-80s.jpg)
vscode配置与python虚拟环境切换的几种方式总结
2023-09-04 07:09:28
![](https://img.aspxhome.com/file/2023/0/76990_0s.jpg)
MySQL多表查询机制
2024-01-25 22:38:33
![](https://img.aspxhome.com/file/2023/3/69843_0s.png)
VUE3中实现拖拽与缩放自定义看板vue-grid-layout详解
2023-07-02 16:28:59
![](https://img.aspxhome.com/file/2023/4/139674_0s.jpg)
vue中beforeRouteLeave实现页面回退不刷新的示例代码
2024-05-09 15:11:16
![](https://img.aspxhome.com/file/2023/2/126562_0s.jpg)
python可视化 matplotlib画图使用colorbar工具自定义颜色
2023-01-06 07:10:01
![](https://img.aspxhome.com/file/2023/2/94022_0s.png)
通过 Django Pagination 实现简单分页功能
2021-03-15 15:53:03
![](https://img.aspxhome.com/file/2023/8/127908_0s.jpg)
TensorFlow2基本操作之合并分割与统计
2022-01-01 21:47:39
![](https://img.aspxhome.com/file/2023/3/103703_0s.jpg)