Specify input type () in Python?

I was still studying, so please excuse me if this sounds like a dumb question. Is it possible to define the time of entry, such as time, date, currency, or does it have to be checked manually? Such as:

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

      

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

Many thanks

+1


source to share


3 answers


input

(in Python 2.any) will return the type of any expression the user enters. Better (in Python 2. any) is to use raw_input

that returns a string and does the conversion on its own, catching TypeError

if the conversion fails.



Python 3.any input

works like 2.any raw_input

, i.e. returns a string.

+8


source


You really can't get a function to input

return a specific type. Your best bet is to write some kind of wrapper that reads some data from the user and then converts it to the appropriate type for your application (or throws an exception on error).



Also, as Alex said, it is better to use raw_input

for user input as it will always return the input value as a string. Much more manageable.

+1


source


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

      

-1


source







All Articles