-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
P01 #2
Comments
__init__ method
Here you’re forgetting to set the ability’s name. There is also an issue with the way that you set the attack strength here. The math should be done in the Ability.attack() method relating to the randint call.
Ability.attack()
One problem is randint is in the random module so must be called using dot notation `random.randint()`. I failed to mention that and have added it to the tutorial.
In addition, the property you’re trying to access is outside the scope of the method. You need to access it through the self variable that is passed in. It works the same as “this” in javascript. This is covered later and I will move it sooner in the tutorial.
Line 10 -> self.attack=random.randint(2, self.attack_strength)
Now this line will run… but it won’t be right.
self.attack = randint(2, attack_strength)
return self.attack
This is setting the function attack as your random value and then returning that. Simply return the value that is given to you from the randint() method.
return random.randint(2, this.attack_strength)
2 needs to be the value that is calculated using your integer division (see line 6 with the // operator)
Ability.attack() should return between 1/2 and the full attack value. So if your ability’s strength is 100, attack() could return 50, but not 2.
The hero’s attack function needs to return the total of all the returned values from each ability it runs the attack method on. I will make the tutorial more clear about this requirement.
For now this is the hero’s attack method
def attack(self):
total = 0
# Run attack() on every ability hero has
for ability in self.abilities:
total += ability.attack()
return total
… On Aug 21, 2018, at 12:22 AM, Joe Rezendes ***@***.***> wrote:
Here was my code when I get stuck.
import random
class Ability:
def __init__(self, name, attack_strength):
# Initialize starting values
self.attack_strength = 20 // 8
def attack(self):
# return attack value
self.attack = randint(2, attack_strength)
return self.attack
def update_attack(self, attack_strength):
# update attack value
self.attack_strength = attack_strength
class Hero:
def __init__(self, name):
# Initialize starting values
self.abilities = list()
self.name = name
def add_ability(self, ability):
# Add ability to abilities list
self.add_ability = self.abilities.append(ability)
def attack(self):
# Run attack() on every ability hero has
for ability in self.abilities:
ability.attack
if __name__ == "__main__":
# If you run this file from the terminal this block is executed.
hero = Hero("Wonder Woman")
print(hero.attack())
ability = Ability("Divine Speed", 300)
hero.add_ability(ability)
print(hero.attack())
new_ability = Ability("Super Human Strength", 800)
hero.add_ability(new_ability)
print(hero.attack())
Here is the error I'm getting in my console:
None
None
Traceback (most recent call last):
File "superheroes.py", line 40, in <module>
hero.add_ability(new_ability)
TypeError: 'NoneType' object is not callable
I think it would be beneficial for me to see the solutions. I'm not sure if I'm doing this right yet.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#2>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AGxJu6T_rQZ0hcN_IzJSFkOYaRMdTiKHks5uS7VCgaJpZM4WFPFv>.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got to the: A Note on Scope and Encapsulation section before I got stuck. A big part of it is not being confident that I'm doing this correctly.
Here was my code when I get stuck.
Here is the error I'm getting in my console:
I think it would be beneficial for me to see the solutions. I'm not sure if I'm doing this right yet.
I will revisit this tomorrow morning. Perhaps we could work together on it?
The text was updated successfully, but these errors were encountered: