Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why Python giving me UnboundLocalError?

My Python program giving me a unbound error but i cant get it why

(Error occurs at connectionstat function)

Here is the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from datetime import datetime
from datetime import date
import requests
g = "noinput"
passhash = open("atlaspass.txt", "r")
systracer = "noinput"
var_tarih = date.today()
var_zaman = datetime.now()
#def fallbackid():
   # print("Atlas yanìt vermiyor")
def about():
           settings_about_version = open('atlasver.txt', 'r')
           settings_about_version.seek(0)     #Dosyayı baştan okur
           print("Atlas Versiyonu: " ,settings_about_version.readline())
           settings_about_codename = open('atlascodename.txt', "r")
           settings_about_codename.seek(0)
           print("Atlas ", settings_about_codename.readline())
           print()
def connectionstat():
    url = "http://google.com"
    timeout = 5
    try:
        request = request.get(url, timeout = timeout)
        print('İnternete Bağlı')
    except (requests.ConnectionError, requests.Timeout) as exception:
        print("İnternet Bağlantısı Yok")
def logon():
    tokenhash = passhash.readline()
    while True:
        atlaslogon = input("Lütfen Şifrenizi Giriniz: ")
        if atlaslogon != tokenhash:
            print('Şifre Yanlış')
        if atlaslogon == tokenhash:
            print("Şifre Doğru")
            break          
def desktop_apps():
    print("[1] - Ayarlar")
def desktop():
    print()
    print()
    print()
    print('Hoşgeldiniz')
    print()
    print()
    zaman = var_zaman.strftime("%H:%M")
    print(zaman ,  var_tarih)
    print()
    desktop_apps()
logon()
desktop()
connectionstat()

I runned the program from Pydroid (I am using Android because i dont have a Personal Computer)

The result is

UnboundLocalError: local variable ‘request’ referenced before assignment

Full Output is

Lütfen Şifrenizi Giriniz: j4debug
Şifre Doğru



Hoşgeldiniz


14:34 2022-11-15

[1] - Ayarlar
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 51, in <module>
  File "<string>", line 23, in connectionstat
UnboundLocalError: local variable 'request' referenced before assignment
[Program finished]

Please explain why Python giving me a unbound error

I am a newbie sorry if i made mistakes

Any solutions?

Thanks

>Solution :

Change this line from request = request.get(url, timeout = timeout) to
request = requests.get(url, timeout = timeout).

Note that the library is called requests and not request.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading