Can you help me with this question in Python? Implement a fight method for the R
ID: 646969 • Letter: C
Question
Can you help me with this question in Python?
Implement a fight method for the Robot class, using the test code shown below to verify it. The fight method should print out information about both challengers - don?t duplicate your work from str!! Then, the method should check to make sure both robots are online. If one is offline, print a message like C3Po cannot fight - it is offline. Compare strength scores. The higher score wins. If both scores are tied, choose randomly (don?t forget to import random)! Print out a message about who wins, then set the loser to be offline so it cannot fight again. Print both robots.Explanation / Answer
Python Program:
class Robot:
"""Represents a robot, with a name."""
# A class variable, counting the number of robots
population = 0
def __init__(self, name):
"""Initializes the data."""
self.name = name
print "(Initializing {})".format(self.name)
# When this person is created, the robot
# adds to the population
Robot.population += 1
def die(self):
"""I am dying."""
print "{} is being destroyed!".format(self.name)
Robot.population -= 1
if Robot.population == 0:
print "{} was the last one.".format(self.name)
else:
print "There are still {:d} robots working.".format(
Robot.population)
def say_hi(self):
"""Greeting by the robot.
Yeah, they can do that."""
print "Greetings, my masters call me {}.".format(self.name)
@classmethod
def how_many(cls):
"""Prints the current population."""
print "We have {:d} robots.".format(cls.population)
droid1 = Robot("R2-D2")
droid1.say_hi()
Robot.how_many()
droid2 = Robot("C-3PO")
droid2.say_hi()
Robot.how_many()
print " Robots can do some work here. "
print "Robots have finished their work. So let's destroy them."
droid1.die()
droid2.die()
Robot.how_many()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.