在 Python 中指定 input() 类型? [英] Specify input() type in Python?

查看:46
本文介绍了在 Python 中指定 input() 类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以定义输入时间,例如时间、日期、货币或应该手动验证的时间?例如:

Is it possible to define input times, like time, date, currency or that should be verified manually? Like for example:

morning = input('Enter morning Time:')
evening = input('Enter evening Time:')

.. 我需要(只)时间在这里,我如何确保用户以 xx:xx 格式输入输入,其中 xx 仅为整数.

.. I need (only) time here, how do I make sure that user enters input in xx:xx format where xx are integers only.

推荐答案

input(在 Python 2.any 中)将返回用户输入的任何表达式的类型.更好(在 Python 2.any 中)是使用 raw_input,返回一个字符串,自己进行转换,捕获TypeError 如果转换失败.

Python 3.any 的 input 与 2.any 的 raw_input 类似,即它返回一个字符串.

Python 3.any's input works like 2.any's raw_input, i.e., it returns a string.

这篇关于在 Python 中指定 input() 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆