首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python 不能有return語句嗎?该怎么解决

2013-07-04 
python 不能有return語句嗎?import stringcheck the identify validenum string.digitsalpha st

python 不能有return語句嗎?

import string
'''check the identify valide'''
num = string.digits
alpha = string.letters + '_'

identify = raw_input('Input the id to check')
if len(identify) < 1:
    print('The len must large than 1')
    return 
    
if identify[0] not in alpha:
    print('The first char is error')
    return

for item in identify[1:]:
    if item not in alpha + num:
        print('The identify contains invalide char')
        return
print('Ok,valide')

第九行,說是return outside function??

[解决办法]

import string
'''check the identify valide'''
num = string.digits
alpha = string.letters + '_'

identify = raw_input('Input the id to check')

def check_valid(msg):
    if len(identify) < 1:
        print('The len must large than 1')
        return False
        
    if identify[0] not in alpha:
        print('The first char is error')
        return False

    for item in identify[1:]:
        if item not in alpha + num:
            print('The identify contains invalide char')
            return False

    return True

if __name__ == "__main__":
    if check_valid(identify):
        print('Ok,valide')
    else:
        print('invalid')

热点排行