读书人

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

发布时间: 2013-07-04 11:45:51 作者: rapoo

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')

读书人网 >perl python

热点推荐