python - 删除输入部分 - PYTHON
问题描述
我正在创建一个数模转换器。我有一个存储用户输入的变量,它应该是一个数字时间(例如 13:00)。然后我执行 inputVariable%12 以获取模拟时钟(例如 1)上的时间。但是,要做到这一点,我需要删除用户提供的任何额外信息(例如:00),所以我只存储了数字。我该怎么做呢?
time = input("Enter digital time here:\ne.g 12:00\n")
#Delete extra information here
time = int(time)
time = time%12
time = str(time)
print(time + " o'clock")
解决方案
我假设您的输入不会有任何错误(例如,用户输入了其他字符、超过 23 小时等)。考虑到您的输入格式,您应该首先拆分:
. 这应该给你两个部分:小时和分钟。在此之后,您可以根据需要进行处理:
time = input("Enter digital time here:\ne.g 12:00\n")
hours, minutes = time.split(':')
hours = int(hours)
hours = hours % 12
hours = str(hours)
print(hours + " o'clock")
推荐阅读
- oracle - 在 Where 子句中使用动态参数的 Oracle PL/SQL 查询
- java - Class.forName(String) 与动态字符串不起作用
- xamarin.forms - 用于接收 SMS 的广播接收器不起作用
- python - Python 自动输入凭据
- javascript - Angularjs多个应用程序无法路由子应用程序
- c - Read() 返回值和 EOF
- angular - Angular 6从WebAPI发送的字节数组打开pdf
- java - 找不到空指针异常
- python - 尝试/排除问题 Python 2.7
- c# - 如何从控制器显示一个简单列表以在视图的选择元素中查看?