Fixing UnboundLocalError in Python while Writing a Code in Pyton

𝑯𝒂𝒗𝒆 𝒚𝒐𝒖 𝒆𝒗𝒆𝒓 𝒆𝒏𝒄𝒐𝒖𝒏𝒕𝒆𝒓𝒆𝒅 𝑼𝒏𝒃𝒐𝒖𝒏𝒅𝑳𝒐𝒄𝒂𝒍 𝑬𝒓𝒓𝒐𝒓 𝒘𝒉𝒊𝒍𝒆 𝒘𝒓𝒊𝒕𝒊𝒏𝒈 𝒂 𝒄𝒐𝒅𝒆 𝒊𝒏 𝒑𝒚𝒕𝒉𝒐𝒏? Last week, I learned about data structures in python and decided to work on a task that required me to write functions that will collect student IDs, Marks, Average score, student with the highest score and determine those who failed or passed. Along the way, I encountered an 𝐔𝐧𝐛𝐨𝐮𝐧𝐝𝐋𝐨𝐜𝐚𝐥 𝐄𝐫𝐫𝐨𝐫. this happed because i used a variable before assigning a value to it. An  𝐔𝐧𝐛𝐨𝐮𝐧𝐝𝐋𝐨𝐜𝐚𝐥 𝐄𝐫𝐫𝐨𝐫 occurs when python tries to access a local variable that has not yet been associated with a value. for example: 𝒅𝒆𝒇 𝑴𝒚_𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏(): 𝒑𝒓𝒊𝒏𝒕(𝒙) 𝒙 = 10 This will raise an  𝐔𝐧𝐛𝐨𝐮𝐧𝐝𝐋𝐨𝐜𝐚𝐥 𝐄𝐫𝐫𝐨𝐫 because 𝐱 is treated as a local variable inside the function. This task helped me better understand: 🔹 Data types and iteration in python 🔹 Message error such as 𝗨𝗻𝗯𝗼𝘂𝗻𝗱𝗟𝗼𝗰𝗮𝗹 𝗘𝗿𝗿𝗼𝗿, 𝗡𝗮𝗺𝗲𝗘𝗿𝗿𝗼𝗿, and 𝗩𝗮𝗹𝘂𝗲𝗘𝗿𝗿𝗼𝗿 This task reinforced an important lesson: 𝐞𝐫𝐫𝐨𝐫𝐬 𝐚𝐫𝐞 𝐧𝐨𝐭 𝐟𝐚𝐢𝐥𝐮𝐫𝐞𝐬, 𝐭𝐡𝐞𝐲 𝐚𝐫𝐞 𝐟𝐞𝐞𝐝𝐛𝐚𝐜𝐤. still learning, still improving.🚀 👉𝑯𝒂𝒗𝒆 𝒚𝒐𝒖 𝒇𝒂𝒄𝒆𝒅 𝒕𝒉𝒊𝒔 𝒆𝒓𝒓𝒐𝒓 𝒃𝒆𝒇𝒐𝒓𝒆? 𝑺𝒉𝒂𝒓𝒆 𝒚𝒐𝒖𝒓 𝒆𝒙𝒑𝒆𝒓𝒊𝒆𝒏𝒄𝒆 𝒊𝒏 𝒕𝒉𝒆 𝒄𝒐𝒎𝒎𝒆𝒏𝒕𝒔. #Python #Datascience #Learning

  • text

I love this sentence "errors are not failures, they are feedback". Well done, Isah MOHAMMED

Like
Reply

To view or add a comment, sign in

Explore content categories