Python Forum
How to fix UnboundLocalError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix UnboundLocalError
#6
(Jan-07-2018, 04:16 PM)Tawnwen Wrote: would you recommend a different method of keeping track of the answers_right, without resorting to a global variable?
You are reaching the point where you have functions that manipulate external data and you need a way to manage both the data and the function. That's why classes were invented. Here is how you can code this by using a class:

class BoneFinder:
    foot_bones = [
        "calcaneus", "talus", "cuboid",
        "navicular", "lateral cuneiform",
        "intermediate cuneiform", "medial cuneiform",
    ]

    def __init__(self):
        self.answers_right = 0
 
    def examine(self, bone_name):
        if bone_name.lower() in self.foot_bones:
            print("correct!")
            self.answers_right += + 1
        else:
            print("incorrect")

finder = BoneFinder()         
finder.examine(input("enter name of a bone: "))
finder.examine(input("enter name of another bone: "))
 
print("number of foot bones correctly identified: ", finder.answers_right)
In this code, a single entity holds the variable answer_right between subsequent calls. The code looks more complicated than your program but in fact, it is easier to extend by adding new features.
Reply


Messages In This Thread
How to fix UnboundLocalError - by Tawnwen - Jan-07-2018, 05:59 AM
RE: How to fix UnboundLocalError - by wavic - Jan-07-2018, 07:57 AM
RE: How to fix UnboundLocalError - by Gribouillis - Jan-07-2018, 08:08 AM
RE: How to fix UnboundLocalError - by Tawnwen - Jan-07-2018, 04:16 PM
RE: How to fix UnboundLocalError - by wavic - Jan-07-2018, 04:31 PM
RE: How to fix UnboundLocalError - by Gribouillis - Jan-07-2018, 05:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How does UnboundLocalError work? deanhystad 3 1,689 Feb-25-2022, 01:21 AM
Last Post: bowlofred
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,925 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  exec + subprocess = UnboundLocalError paul18fr 6 3,537 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,282 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  UnBoundLocalError Seaninho 3 2,711 May-31-2020, 07:22 AM
Last Post: azajali43
  UnboundLocalError: local variable referenced before assignment svr 1 3,322 Dec-27-2019, 09:08 AM
Last Post: perfringo
  UnboundLocalError: local variable ' ' referenced before assignment d3fi 10 5,593 Sep-03-2019, 07:22 PM
Last Post: buran
  'Exception Has occured: UnBoundLocalError' caston 1 2,473 Jun-12-2019, 02:33 PM
Last Post: perfringo
  UnboundLocalError Problem DrChicken24 1 2,238 Mar-27-2019, 02:53 AM
Last Post: ichabod801
  UnboundLocalError, how to fix it please? etrius 4 3,914 Jan-18-2018, 09:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020