Jan-07-2018, 05:38 PM
(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.