简体   繁体   中英

What am I doing wrong with my object functions?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM