Please support our Python advertiser: Programming Forums
Views: 1617 | Replies: 9
![]() |
•
•
Join Date: May 2005
Posts: 215
Reputation:
Rep Power: 4
Solved Threads: 0
I am having a tough time understanding what is going on in this following code
[php]
class Player(object):
""" A player in a shooter game. """
def blast(self, enemy):
print "The player blasts an enemy.\n"
enemy.die()
class Alien(object):
""" An alien in a shooter game. """
def die(self):
print "The alien gasps and says, 'Oh, this is it. This is the big one. \n" \
"Yes, it's getting dark now. Tell my 1.6 million larvae that I loved them... \n" \
"Good-bye, cruel universe.'"
# main
print "\t\tDeath of an Alien\n"
hero = Player()
invader = Alien()
hero.blast(invader)
[/php]
in particular this line
[php]hero.blast(invader)[/php]
how is the object invader interacting the the blast method?
[php]
class Player(object):
""" A player in a shooter game. """
def blast(self, enemy):
print "The player blasts an enemy.\n"
enemy.die()
class Alien(object):
""" An alien in a shooter game. """
def die(self):
print "The alien gasps and says, 'Oh, this is it. This is the big one. \n" \
"Yes, it's getting dark now. Tell my 1.6 million larvae that I loved them... \n" \
"Good-bye, cruel universe.'"
# main
print "\t\tDeath of an Alien\n"
hero = Player()
invader = Alien()
hero.blast(invader)
[/php]
in particular this line
[php]hero.blast(invader)[/php]
how is the object invader interacting the the blast method?
The last three lines of your code are equal to writing ...
Maybe this shows the connection a little better. Player().blast has the line enemy.die() in it. We tell blast that the enemy is the Alien(), so it calls Alien().die().
BTW, the print statement with the three lines of text will give you an error. Change it to a triple quote, or add two more print.
Player().blast(Alien())
BTW, the print statement with the three lines of text will give you an error. Change it to a triple quote, or add two more print.
May 'the Google' be with you!
Testing ...
class Alien(object):
""" An alien in a shooter game. """
def die(self):
# adding three lines to one print statement
print "The alien gasps and says, 'Oh, this is it. This is the big one. \n"\
"Yes, it's getting dark now. Tell my 1.6 million larvae that I loved them... \n"\
"Good-bye, cruel universe.'" May 'the Google' be with you!
[php]class Alien(object):
""" An alien in a shooter game. """
def die(self):
# adding three lines to one print statement
print "The alien gasps and says, 'Oh, this is it. This is the big one. \n"\
"Yes, it's getting dark now. Tell my 1.6 million larvae that I loved them... \n"\
"Good-bye, cruel universe.'"
[/php]
I just figured that out too!
Thanks ...
I just put a hint into the Starting Python sticky about multiline strings. It looks like the php code field doesn't like trailing \ characters on a line! Bummer, I like the color!!!
Edit: I informed the gurus at DaniWeb about this. Let's see if anything can be done about it.
""" An alien in a shooter game. """
def die(self):
# adding three lines to one print statement
print "The alien gasps and says, 'Oh, this is it. This is the big one. \n"\
"Yes, it's getting dark now. Tell my 1.6 million larvae that I loved them... \n"\
"Good-bye, cruel universe.'"
[/php]
I just figured that out too!
Thanks ...
I just put a hint into the Starting Python sticky about multiline strings. It looks like the php code field doesn't like trailing \ characters on a line! Bummer, I like the color!!!
Edit: I informed the gurus at DaniWeb about this. Let's see if anything can be done about it.
May 'the Google' be with you!
This is actually the exact same chapter I am working on in Python for the Absolute Beginner (Chapter 9). The hero invokes its blast method, with invader as its argument. The hero's blast method invokes the invader's die method, which prints the death message.
Hope that helps. I'm new at this as well.
Hope that helps. I'm new at this as well.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode