Reputation: 1
So my goal is to create a quiz. I first went about it with global variables but despite using return and global indicator I couldn't get it to work so I went to classes and objects. Below is a small sample with an answer to the first question.
print('Hello, Welcome to the what bender are you test!')
print('Answer the questions one at a time in order and in lower case. When youre done
answering all of them type Done to get your results. Ready? Lets go!')
print('What is your favorite rainbow color? Indigo and Violet are in here too.')
print('Do you know your blood type? Just answer yes or no ^^')
print('What is your Hogwarts House?')
print('Out of these four animals which do you prefer? Deer, Ram, Longhorn
Hummingbird')
class Benders():
def __init__(self,points = 0):
self.points = points
Firebender = Benders()
Airbenders = Benders()
Waterbenders = Benders()
Earthbenders = Benders()
def red():
setattr(Firebender.Points,1)
I dont know what to do, I can't leave red in the class functions or people cant type in the answer to change the attribute. I've also tried putting the class in the parenthesis to clarify for python. Please help
Upvotes: 0
Views: 45
Reputation: 531315
setattr
takes the object and the attribute name as separate arguments:
setattr(Firebender, 'points', 1)
but it's simpler to just write
Firebender.points = 1
setattr
is more useful when the name of the attribute you want to change is stored in a variable, and so you can't use the dot notation:
attribute = "points"
# Firebender.attribute = 1 # sets attribute, not points
setattr(Firebender, attribute, 1) # OK
Upvotes: 1