【问题标题】:Python try except finally invalid syntax errorPython尝试除了最终无效的语法错误
【发布时间】:2019-04-14 00:03:45
【问题描述】:

所以我试图在 python 中使用这种异常处理。我为此使用 python2.7 和烧瓶。另外我对python和flask都是新手,所以我一定在这里做错了。

if test:
    cursor = conn.cursor()
    try:
        print cursor.execute("INSERT INTO Users (email, password, firstname, lastname, home, gender, dob, bio, profile_Image) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}')".format(email, password, firstname, lastname, home, gender, dob, bio, photo_data))
        conn.commit()
        #log user in
        user = User()
        user.id = email
        flask_login.login_user(user)
        uid = getUserIdFromEmail(flask_login.current_user.id)
        today = str(date.today())
        print today
    except Exception as e:
        print e
        print "Something Went wrong"
        return flask.redirect(flask.url_for('register'))
    print cursor.execute("INSERT INTO Album (uid, aname, adate, cover) VALUES ('{0}', 'default', '{1}', '{2}')".format(uid, today, photo_data))
    aid = getAIDfromAname('default', uid)
    cursor.execute("INSERT INTO Photo (uid, aid, data, caption) VALUES ('{0}', '{1}', '{2}', 'profile')".format(uid,aid,photo_data))
    cursor.execute("INSERT INTO Scoreboard (uid) VALUES ('{0}')".format(uid))
    conn.commit()
    finally:
        cursor.close()
    return render_template('profile.html', firstname=firstname, message='Account Created!')

else:
    print "couldn't find all tokens"
    return render_template('register.html', message='Email Already Exists')

如果我运行应用程序,它会给我这个错误

  File "app.py", line 540
finally:
      ^SyntaxError: invalid syntax

我想知道为什么它给了我错误:/

【问题讨论】:

  • 您的缩进已关闭。 try/exept 块以它下面的缩进打印结束。

标签: python exception flask finally


【解决方案1】:
if test:
    cursor = conn.cursor()
    try:
        print cursor.execute("INSERT INTO Users (email, password, firstname, 
lastname, home, gender, dob, bio, profile_Image) VALUES ('{0}', '{1}', '{2}', 
'{3}', '{4}', '{5}', '{6}', '{7}', '{8}')".format(email, password, firstname, 
lastname, home, gender, dob, bio, photo_data))
        conn.commit()
        #log user in
        user = User()
        user.id = email
        flask_login.login_user(user)
        uid = getUserIdFromEmail(flask_login.current_user.id)
        today = str(date.today())
        print today
    except Exception as e:
        print e
        print "Something Went wrong"
        print cursor.execute("INSERT INTO Album (uid, aname, adate, cover) VALUES ('{0}', 'default', '{1}', '{2}')".format(uid, today, photo_data))
        aid = getAIDfromAname('default', uid)
        cursor.execute("INSERT INTO Photo (uid, aid, data, caption) VALUES ('{0}', '{1}', '{2}', 'profile')".format(uid,aid,photo_data))
        cursor.execute("INSERT INTO Scoreboard (uid) VALUES ('{0}')".format(uid))
        conn.commit()
        return flask.redirect(flask.url_for('register'))
    finally:
        cursor.close()
    return render_template('profile.html', firstname=firstname, message='Account 
Created!'))

else:
    print "couldn't find all tokens"
    return render_template('register.html', message='Email Already Exists'

这会奏效。 except 块内“print cursor.execute”行的缩进不正确。

【讨论】:

  • 期望块中return语句之后的代码永远不会执行。
  • 谢谢。菜鸟失误。编辑了代码。抱歉,我一定是看完了整个代码。
猜你喜欢
  • 2014-08-22
  • 2019-12-09
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多