简体   繁体   中英

I keep getting an error: UnboundLocalError: local variable 'TweetID' referenced before assignment [SOLVED]

I'm having a problem with an error: UnboundLocalError: local variable 'TweetID' referenced before assignment.

Now please, don't crap on the layout and functions of this code. I posted the entire code just in case since I don't know where the mistake is. I'm still new and I know it makes no sense, but 99% works, except this part. I really can't find the reason of the error, I know what it means but I don't see the problem.

A little background: I have to pull a certain tweet from a database and accept or reject it. When Accepted, it tweets to twitter. When rejected, you leave an explanation why. Kind of like a moderator. Moderator ID and StationID (where the tweet was posted) are also sent to the database.

If you need more info I'm happy to supply. Other constructive feedback is also appreciated, but I'm a real newbie so I can't deal with anything too complicated:)

Traceback (most recent call last): File "D:\Users\vince\Desktop\Python Code Project Steam\Twitterzuil\MOD.py", line 74, in Process() File "D:\Users\vince\Desktop\Python Code Project Steam\Twitterzuil\MOD.py", line 68, in Process Accept() File "D:\Users\vince\Desktop\Python Code Project Steam\Twitterzuil\MOD.py", line 32, in Accept TweetID = TweetID() UnboundLocalError: local variable 'TweetID' referenced before assignment

import psycopg2
import time


conn = psycopg2.connect(
    host="localhost",
    database="postgres",
    user="postgres",
    password="")

cur = conn.cursor()

cur.execute("SELECT (Tweet, naam, inhoud, datum, tijd) from Bericht2 WHERE acceptrejectposted is NULL ORDER BY tijd ASC LIMIT 1")
result = cur.fetchall();

#login
mod_id = int(input("Geef uw moderator ID: "))

#VIEW TWEET
print(result)

def TweetID():
    cur.execute("SELECT (Tweet) from Bericht2 WHERE acceptrejectposted is NULL ORDER BY tijd ASC LIMIT 1")
    tweet_id = cur.fetchall()
    return tweet_id

def Accept():
    tweet_id = TweetID()
    cur.execute(f"UPDATE Bericht2 SET acceptrejectposted = 1 WHERE tweet = {tweet_id}")
    cur.execute(f"UPDATE Bericht2 SET moderator2mod_id = {mod_id} WHERE tweet = {tweet_id}")
    print('TWEET ACCEPTED')
    conn.commit()
    PostTweet()

def Reject():
    tweet_id = TweetID()
    cur.execute(f"UPDATE Bericht2 SET acceptrejectposted = 0 WHERE tweet = {tweet_id}")
    cur.execute(f"UPDATE Bericht2 SET moderator2mod_id = {mod_id} WHERE tweet = {tweet_id}")
    Opmerking = input("Opmerking?")
    cur.execute(f"UPDATE Bericht2 SET opmerking = '{Opmerking}' WHERE tweet = {tweet_id}")
    print('TWEET REJECTED')
    conn.commit()

def PostTweet():
    tweet_id = TweetID()
    cur.execute("SELECT (inhoud) from Bericht2 WHERE acceptrejectposted = 1 ORDER BY tijd ASC LIMIT 1")
    Tweet = cur.fetchall()
    cur.execute(f"UPDATE Bericht2 SET acceptrejectposted = 2 WHERE tweet = {tweet_id}")
    conn.commit()
    print("Posting to twitter...")
    time.sleep(1)
    print("Posted!")

def Process():
    AccRej = input("Accept or Reject? ")
    if AccRej == "Accept":
        Accept()
    elif AccRej == "Reject":
        Reject()
    else:
        print("Invalid")

Process()

As Sylvester Kruin correctly noticed, you should not mixing variable and function names. Consider snake_case for variable names:

def Accept():
  tweet_id = TweetID()
  cur.execute(f"UPDATE Bericht2 SET acceptrejectposted = 1 WHERE tweet = {tweet_id}")
  cur.execute(f"UPDATE Bericht2 SET moderator2mod_id = {mod_id} WHERE tweet = {tweet_id}")
  print('TWEET ACCEPTED')
  conn.commit()
  PostTweet()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM