Python中文
首页
教程
问答
标签
搜索
登录
注册
这个python代码有什么问题?if块没有被执行
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在编写一个模块,在这个模块中,电子邮件字符串在被插入数据库之前会被验证。当我尝试输入无效的电子邮件字符串时,它会打印带有错误电子邮件消息的else块,但当我输入正确的电子邮件字符串时,它不会执行任何操作。代码如下:</p> <pre><code> #!/usr/bin/python import MySQLdb import re # Open database connection db = MySQLdb.connect("localhost","root","root","acl" ) # prepare a cursor object using cursor() method cursor = db.cursor() def addUser(email,password): try: if validateEmail(email): sql = "INSERT INTO acl_users(email, password) VALUES ('%s', '%s')" % (email, password) # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() else: print "wrong email" except Exception as inst: # Rollback in case there is any error db.rollback() print inst def validateEmail(email): if len(email) > 7: if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None: return True else: return False else: return False </code></pre> <p>有什么建议吗?你知道吗</p> <h2>编辑-1</h2> <p>找到答案了伙计们!在except块中创建异常实例之后,我知道import re丢失了。解决了问题。你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>在validateEmail中,您使用模块re,但没有导入它。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何将python输出重定向到python控制台和Windows中的文本文件
10 回答
如何将Python运行时嵌入运行在Windows上的R包中
2 回答
如何将python进程作为另一个Windows us运行
4 回答
如何将Python进程的输出用Python管道传输?
1 回答
如何将Python进程的输出重定向到Rust进程?
3 回答
如何将python连接到Azure云并创建Azure数据工厂
2 回答
如何将Python连接到Db2
4 回答
如何将python连接到IBMDB2?
10 回答
如何将Python连接到microsoftaccess数据库文件?
9 回答
如何将python连接到MySQL服务器
5 回答
如何将Python连接到Node.js?
9 回答
如何将python连接到Oracle Application Express
9 回答
如何将Python连接到PostgreSQL
3 回答
如何将Python连接到Postgres服务器?
4 回答
如何将Python连接到SAS Enterprise Guide(EG)服务器
9 回答
如何将Python连接到Spark会话并保持RDDs的Ali
5 回答
如何将python连接到sqlite3并在上填充多行
9 回答
如何将python连接到使用docker运行的cassandra
10 回答
如何将python退格应用于字符串
10 回答
如何将python逻辑应用到tkinter GUI中?这是一个简单的GET请求程序
3 回答