本文介绍了编写一个读取单个字符串的程序,并确定是否作为密码有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这个问题是我的家庭作业。我不是在找人告诉我为什么我的代码不起作用的答案。正在寻找如何解决这个问题的正确方向。当我运行这个时,我得到 '返回'外部功能:< string>,第25行 我不明白为什么会这样。有人能帮助我吗? 如下代码见: import re def 验证(密码): # 计算长度 length_error = 不(len(密码)< = 12 和 len(密码)> = 6 ) # 搜索数字 d_error = re.search(r \d,密码) 无 # 搜索大写 ucase_error = re.search(r [AZ],密码) 无 # 搜索小写 lcase_error = re.search(r [az ],密码) 无 # 搜索符号 sym_error = re.search(r [$#@ + r ' ]',密码) 无 # 总体结果 password_ok = 不(length_erro r 或 digit_error 或 uppercase_error 或 lowercase_error 或 symbol_error) print ' password_ok',password_ok,' \ nlength_error',length_error,' \ nd_error',digit_error,' \ nucase_error',ucase_error,' \ nncase_error',lowercase_error,' \ nsns_error' ,symbol_error return ' password_ok' def main(): # 将cmd设置为除quit()之外的任何内容 cmd = ' ' # 处理用户命令 cmd = input(' > ') 而 cmd!= ' 退出':密码= cmd 如果验证(密码): print 密码有效 else : print 密码无效 cmd =输入(' >') main() 我有什么尝试过: 我觉得我已经尝试了所有的东西,但不确定为什么这段代码不能正常工作。解决方案 #@ + r ' ]',密码)是 无 # 整体结果 password_ok = 不(length_error 或 digit_error 或 uppercase_error 或 lowercase_error 或 symbol_error) print ' password_ok',password_ok,' \ nlength_error',length_error,' \ nd_error',digit_error,' \ nucase_error',ucase_error,' \ nncase_error',lowercase_error,' \ nnsym_error',symbol_error return ' password_ok' def main(): # 将cmd设置为除quit()之外的任何内容 cmd = ' ' # 处理用户命令 cmd = input(' > ') 而 cmd!= ' 退出':密码= cmd 如果验证(密码): print 密码有效 else : print 密码无效 cmd =输入(' >') main() 我有什么尝试过: 我觉得我已经尝试了所有东西,但不确定为什么这段代码不能正常工作。 Python的基本语法是用缩进组织代码。正如错误所说,除了第一行之外,在validate(密码)函数下的那些代码行没有缩进,这使得它们在函数之外,包括return语句。 main()也是如此。此外,不同版本的Python之间的打印方法的构造也存在差异。您应该参考官方文档和在线教程以获得正确的学习。我不是在评论你的代码逻辑,我怀疑它会起作用。一步一步。祝你好运。 This problem is a homework assignment that I have. I'm not looking for someone to tell me the answer as to why my code isnt working. Looking for push in the right direction on how to fix this. When I run this, I get 'return' outside function: <string>, line 25I don't understand why this happens. Would anyone be able to help me?See below for code:import redef validate(password):# calculating the length length_error = not( len(password) <= 12 and len(password) >= 6)# searching for digitsd_error = re.search(r"\d", password) is None# searching for uppercaseucase_error = re.search(r"[A-Z]", password) is None# searching for lowercaselcase_error = re.search(r"[a-z]", password) is None# searching for symbolssym_error = re.search(r"[$#@"+r'"]', password) is None# overall resultpassword_ok = not ( length_error or digit_error or uppercase_error or lowercase_error or symbol_error ) print 'password_ok ',password_ok,'\nlength_error ',length_error,'\nd_error ',digit_error,'\nucase_error ',ucase_error,'\nlcase_error ',lowercase_error,'\nsym_error ',symbol_errorreturn 'password_ok'def main():# set cmd to anything except quit() cmd = ' ' # process the user commandscmd = input(' > ')while cmd != 'quit': password = cmdif validate(password): print "Password is valid"else: print "Password is invalid"cmd = input(' > ')main()What I have tried:I've feel like I've tried everything, but unsure why this code wont work. 解决方案 #@"+r'"]', password) is None# overall resultpassword_ok = not ( length_error or digit_error or uppercase_error or lowercase_error or symbol_error ) print 'password_ok ',password_ok,'\nlength_error ',length_error,'\nd_error ',digit_error,'\nucase_error ',ucase_error,'\nlcase_error ',lowercase_error,'\nsym_error ',symbol_errorreturn 'password_ok'def main():# set cmd to anything except quit() cmd = ' ' # process the user commandscmd = input(' > ')while cmd != 'quit': password = cmdif validate(password): print "Password is valid"else: print "Password is invalid"cmd = input(' > ')main()What I have tried:I've feel like I've tried everything, but unsure why this code wont work.The very basic syntax of Python is that the code is organized with indentation. As the error said, those lines of code, apart from the first one, under the function of validate(password) are not indented, that made them outside of the function, including the return statement. The same goes for the main(). In addition, there are differences in the construct of the print method between different versions of Python. You should refer to the official documentation and online tutorials for proper learning. I'm not commenting on your logic of the code, I doubt it will work. Take one step at a time. Good luck. 这篇关于编写一个读取单个字符串的程序,并确定是否作为密码有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 20:11