小编Sav*_*avo的帖子

在Python中验证用户输入的正确方法

我正在通过一些Python教程工作,其中一个不断出现的事情是用户输入,我只是想检查一下我是否正确验证它,而不是长篇大论.

我已经编写了下面的代码,只需要询问当天的月份和年份,但如果我需要开始询问地址电话号码等等,这会增长和增长是正常的吗?

def get_input( i ):

    while True:
        # We are checking the day
        if i == 'd':
            try:
                day = int( raw_input( "Please Enter the day: " ) )
                # If the day is not in range reprint
                if day > 0 and day < 32:
                    #Need to account for short months at some point
                    return day
                else:
                    print 'it has to be between 1 and 31'
            except ( ValueError ):
                print "It has to be a number!"
        elif …
Run Code Online (Sandbox Code Playgroud)

python validation user-input

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

user-input ×1

validation ×1