Specify input() type in Python?

后端 未结 2 974
余生分开走
余生分开走 2020-12-06 21:41

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:\         


        
2条回答
  •  太阳男子
    2020-12-06 21:55

    You can't really force the input function to return a certain type. It's best you write some kind of a wrapper that reads some input from the user and then converts it to an appropriate type for your application (or throw an exception in case of an error).

    Also, as Alex said, it's better to use raw_input for user input since it will always return the entered value as a string. Much more manageable.

提交回复
热议问题