python库供用户输入
问题内容:
我正在python中实现一个小的命令行工具,需要向用户询问几个问题。我用
raw_input('Are you male or female?')
每时每刻。现在,我希望能够与愚蠢的用户(或那些懒于阅读/记住文档的用户)打交道,因此我需要检查答案是否有意义。
gender = ''
while gender not in ['male', 'female']:
gender = raw_input('Are you male or female?')
我想知道是否存在像argparse这样的东西可以自动解决这个问题,例如
import inputparse
gender = inputparse.get_input(prompt='Are you male or female?', type=str, possible_input=['male', 'female'])
并会照顾自动检查等?
问题答案: