Join the QuestionCove community and study together with friends!
Sign Up
Gucchi:
and this is my story:
I want to tell you about my superhero.
His name is firefly and his super power is throwing fireballs and shooting lasers.
As he goes against Doctor Wicked, he shouts out his motto
Fire will always win! Then quickly catches up to Doctor Wicked, so he can finish him off!
The villain is defeated when he is lit by burning fire and starts to faint!
Firefly has now earned an additional 200 strength because of his courageous actions
He now has a total of 500 strength and has saved his city from harm.
Gucchi:
i really want to know if i have written everything correctly so far
I think the code looks good so far. Make sure you include all the parameters you need when calling the superhero method so that everything gets defined like you want it to be
Gucchi:
@smokeybrown wrote:
I think the code looks good so far. Make sure you include all the parameters you need when calling the superhero method so that everything gets defined like you want it to be
so when it says "Come up with two additional attributes and one method" , i only have to add these and put everything else in print statements?
Still Need Help?
Join the QuestionCove community and study together with friends!
Yeah it looks like you have a method for defining the hero and a method for adding strength so with one more method you’d fulfill the rewuirements
SmokeyBrown:
Since you included the attribute for villain in the definition method, you also need to include the attribute when you define the hero by calling the method. In the definition of the method, the villain variable can be set to the empty string.
Then you’d define your hero as
firehero = superhero(“Firefly”, 0, “Firefly will always win!”, “Doctor Wicked”)
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
Gucchi:
@smokeybrown wrote:
Yeah it looks like you have a method for defining the hero and a method for adding strength so with one more method you’d fulfill the rewuirements
wait so are the methods the same as attributes>
SmokeyBrown:
No, attributes are the same as parameters. That’s information you feed into a method to determine how it behaves. Like name is an attribute or parameter of the Superhero definition method, and you can set it as “Firefly” or any other name when you call the method
No, attributes are the same as parameters. That’s information you feed into a method to determine how it behaves. Like name is an attribute or parameter of the Superhero definition method, and you can set it as “Firefly” or any other name when you call the method
ohh ok
SmokeyBrown:
You define firehero with a villain attribute on line 18, but fireHero (with a capital H) on line 17 has no such attribute. So, when you try to find the villain attribute of fireHero on line 21 it isn’t recognized
Still Need Help?
Join the QuestionCove community and study together with friends!
This is actually a technique called overloading, which is when you have two or more methods in the same class with the same name but different attributes; the behavior of the method depends on which attributes you pass in that case
For this exercise, we probably don’t need to use overloading, but I thought it would be interesting to share that information
Gucchi:
@smokeybrown wrote:
This is actually a technique called overloading, which is when you have two or more methods in the same class with the same name but different attributes; the behavior of the method depends on which attributes you pass in that case
For this exercise, we probably don’t need to use overloading, but I thought it would be interesting to share that information
yeah we dont really need to use it
ShortlyAfterwards:
@smokeybrown wrote:
This is actually a technique called overloading, which is when you have two or more methods in the same class with the same name but different attributes; the behavior of the method depends on which attributes you pass in that case
For this exercise, we probably don’t need to use overloading, but I thought it would be interesting to share that information
very interesting actually, i was having the same problem with my print statements/output for my website..
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
SmokeyBrown:
That’s interesting. Maybe if you comment out line 17 and leave line 18?
Could be that defining fireHero without villain first gets it stuck to not include that attribute?
You wouldn’t, since you set the value of villain when you call the method to define the superhero
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
Gucchi:
@smokeybrown wrote:
You wouldn’t, since you set the value of villain when you call the method to define the superhero
ok
Gucchi:
all these other lines from the story are just printing right? i dont think that would take long:
I want to tell you about my superhero.
His name is firefly and his super power is throwing fireballs and shooting lasers.
As he goes against Doctor Wicked, he shouts out his motto
Fire will always win! Then quickly catches up to Doctor Wicked, so he can finish him off!
The villain is defeated when he is lit by burning fire and starts to faint!
Firefly has now earned an additional 200 strength because of his courageous actions
He now has a total of 500 strength and has saved his city from harm.
Gucchi:
like the ones without methods or anything
SmokeyBrown:
Yeah I think the only other place you would need to call methods would be for adding strength points. Unless you needed to include another method
I’m still not sure about the bug you’re seeing. It looks like you defined the villain attribute and included it properly…
Could I see your method definition again?
Gucchi:
like the pictures?
Still Need Help?
Join the QuestionCove community and study together with friends!
Yeah I think the only other place you would need to call methods would be for adding strength points. Unless you needed to include another method
I’m still not sure about the bug you’re seeing. It looks like you defined the villain attribute and included it properly…
Could I see your method definition again?
i really dont know either im trying everything
Gucchi:
@smokeybrown wrote:
Yeah I think the only other place you would need to call methods would be for adding strength points. Unless you needed to include another method
I’m still not sure about the bug you’re seeing. It looks like you defined the villain attribute and included it properly…
Could I see your method definition again?
Nice catch!
To call the method for adding points like you defined, you could call the addStrengthPts method with the “self” attribute as the variable referring to your hero and the “points” attribute as the number of points you want to add
SmokeyBrown:
Like
addedPoints=200
addStrengthPts(fireHero, addedPoints)
And then you can print your message saying how many points the hero gained and what their new number of points is
Gucchi:
@smokeybrown wrote:
Nice catch!
To call the method for adding points like you defined, you could call the addStrengthPts method with the “self” attribute as the variable referring to your hero and the “points” attribute as the number of points you want to add
ok so if i were to add like 200 strength points to the superhero i would do something like fireHero.addStrengthPts(200)?
Gucchi:
@smokeybrown wrote:
Like
addedPoints=200
addStrengthPts(fireHero, addedPoints)
And then you can print your message saying how many points the hero gained and what their new number of points is
oh ok
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
Gucchi:
hold on lemme do it
Gucchi:
sorry for the wait, just finished eating
SmokeyBrown:
No worries, take your time :)
Gucchi:
would i add this
"addedPoints=200
addStrengthPts(fireHero, addedPoints)"
right after this code in the image?
Sorry, I meant in the main method you would call the addStrengthPts method from the Superhero class. You don't need to change the methods you already wrote, just make sure to call Superhero.addStrengthPts(...) instead of just addStrengthPts so the program knows where to look for that method (in the Superhero class)
Join the QuestionCove community and study together with friends!
Sign Up
SmokeyBrown:
Oh, you called fireHero as an attribute of the addStrengthPts method on line 17 before you define fireHero on line 22.
If you want to reference a variable, it has to be after that variable is defined. That also applies to the addedPoints attribute; you define that variable on line 19.
Change the order so Superhero.addStrengthPts... appears later, and you can also get rid of line 20, addStrengthPts... since we're not calling the method that way
addedPoints still needs to be defined before the method which calls it as an attribute.
If you put addedPoints before Superhero.addStrengthPts(...) the error message shouldn't appear.
Alternatively, you can forget about the addedPoints variable and just enter the number of points as an attribute directly
Superhero.addStrengthPts(fireHero, 200)
but using the variable might be useful if you want to reference it later, like in your printed message
Gucchi:
@smokeybrown wrote:
addedPoints still needs to be defined before the method which calls it as an attribute.
If you put addedPoints before Superhero.addStrengthPts(...) the error message shouldn't appear.
Alternatively, you can forget about the addedPoints variable and just enter the number of points as an attribute directly
Superhero.addStrengthPts(fireHero, 200)
but using the variable might be useful if you want to reference it later, like in your printed message
oh ok, sorry it was a bit confusing for me, i got it now
Still Need Help?
Join the QuestionCove community and study together with friends!
class Superhero:
# Superhero class represents the facts related to a superhero.
def __init__(self, name = "", strengthPts = 0, motto = " ", villain = " "):
# Create a new Superhero with a name and other attributes
self.name = name
self.strengthPts = strengthPts
self.motto = motto
self.villain = villain
def addStrengthPts(self, points):
# Adds points to the superhero's strength.
self.strengthPts = self.strengthPts + points
def main():
fireHero = Superhero(“Firefly”, 0, “Firefly will always win!”, “Doctor Wicked”)
print("I want to tell you about my superhero!")
print(fireHero.name + "'s motto is " + fireHero.motto)
print("As " + fireHero.name + " goes against " + fireHero.villain + ", he shouts out his motto ")
addedPoints=200
Superhero.addStrengthPts(fireHero, addedPoints)
main()
Gucchi:
my full code
SmokeyBrown:
Looks good!
As far as I understand the assignment, it looks like you fulfilled the requirements
Still Need Help?
Join the QuestionCove community and study together with friends!
"Firefly has now earned an additional 200 strength because of his courageous actions" for this line, dont i have to write it differently, or do i just put it in a print statement?
SmokeyBrown:
@gucchi wrote:
its not really a big issue but there is no space appearing on the output between "win!" and "Then"
You can add a space at the end of the "...win!" string or at the beginning of the "Then..." string.
Either "... win! " or " Then..." would make a space when they're printed together
SmokeyBrown:
@gucchi wrote:
"Firefly has now earned an additional 200 strength because of his courageous actions" for this line, dont i have to write it differently, or do i just put it in a print statement?
That should work fine.
If you want to reference the value in the addedPoints variable you could say
print(""Firefly has now earned an additional " + str(addedPoints) + " strength because of his courageous actions")
but using the number as part of the string like you have it works as well
Gucchi:
@smokeybrown wrote:
@gucchi wrote:
its not really a big issue but there is no space appearing on the output between "win!" and "Then"
You can add a space at the end of the "...win!" string or at the beginning of the "Then..." string.
Either "... win! " or " Then..." would make a space when they're printed together
thank you i solved it
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
SmokeyBrown:
Alright nice!
Gucchi:
i put this code but that reference thing showed up again