Question

In: Computer Science

Python I want to name my hero and my alien in my code how do I...

Python

I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name

import random

class Hero:

    def __init__(self,ammo,health):

        self.ammo=ammo

        self.health=health

    def blast(self):

        print("The Hero blasts an Alien!")

        if self.ammo>0:

            self.ammo-=1

            return True

        else:

            print("Oh no! Hero is out of ammo.")

            return False

    def damage(self):

        self.health-=1

        if self.health==0:

            print("Hero is out of health. Alien wins.")

        else:

            print("Hero took damage")

class Alien:

    def __init__(self,ammo,health):

        self.ammo=ammo

        self.health=health

    def blast(self):

        print("Alien is blasting")

        if self.ammo>0:

            self.ammo-=1

            return True

        else:

            print("Oh no! Alien is out of ammo.")

            return False

    def damage(self):

        self.health-=1

        if self.health==0:

            print("Alien is out of health. Hero wins.")

        else:

            print("Alien took damage")

def main():

    h=Hero(10,10)

    a=Alien(10,10)

    while h.health>0 and a.health>0:

        move=random.randint(0,1)

        if move==0:

            if h.blast():

                a.damage()

            else:

                if a.blast():

                    h.damage()

main()

Solutions

Expert Solution

add name in Alien and Hero classes as in below.. give your name in the main method.. you can see it from the below screens.. make sure that you follow proper indentations...

code:

import random

class Hero:

def __init__(self,ammo,health,name):

self.ammo=ammo

self.health=health

self.name = name

def blast(self):

print(self.name+" blasts an Alien!")

if self.ammo>0:

self.ammo-=1

return True

else:

print("Oh no! "+self.name+" is out of ammo.")

return False

def damage(self):

self.health-=1

if self.health==0:

print(self.name+" is out of health. Alien wins.")

else:

print(self.name+" took damage")

class Alien:

def __init__(self,ammo,health,name):

self.ammo=ammo

self.health=health

self.name = name

def blast(self):

print(self.name+" is blasting")

if self.ammo>0:

self.ammo-=1

return True

else:

print("Oh no! "+self.name+" is out of ammo.")

return False

def damage(self):

self.health-=1

if self.health==0:

print(self.name+" is out of health. Hero wins.")

else:

print(self.name+" took damage")

def main():

h=Hero(10,10,"Iron Man")

a=Alien(10,10,"Thanos")

while h.health>0 and a.health>0:

move=random.randint(0,1)

if move==0:

if h.blast():

a.damage()

else:

if a.blast():

h.damage()

main()


Related Solutions

This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
i want a historical review on movie my name is khan
i want a historical review on movie my name is khan
This is my code I want the average temp for the week to be printed when...
This is my code I want the average temp for the week to be printed when the user types : 'week' currently when the user types  'week' it only prints  Monday - Sunday and the average temp for each day. import java.util.Arrays; import java.util.ArrayList; import java.util.Scanner; public class weeklytemps {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);                  ArrayList Day = new ArrayList(Arrays.asList(    "Monday","Tuesday","Wednesday","Thurday","Friday","Saturday","Sunday")); // Stores days of the week...
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
I have an unexpected indent with my python code. please find out whats wrong with my...
I have an unexpected indent with my python code. please find out whats wrong with my code and run it to show that it works here is the code : def main(): lis = inputData() customerType = convertAcct2String(lis[0]) bushCost = getBushCost(lis[0],int(lis[1],10)) flowerCost = getFlowerBedCost(int(lis[2],10),int(lis[3],10)) fertiCost = getFertilCost(int(lis[4],10)) totalCost = calculateBill(bushCost,fertiCost,flowerCost) printReciept(customerType,totalCost,bushCost,fertiCost,flowerCost) def inputData(): account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage = input("Please enter values").split() return [account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage] def convertAcct2String(accountType): if accountType== "P": return "Preferred" elif accountType == "R": return "Regular" elif accountType == "N": return...
code in python I want to make a function that adds all the multipliers together. The...
code in python I want to make a function that adds all the multipliers together. The code is posted below and please look at wanted output section to see what I want the code to output. The last line of the code is the only addition I need. Thanks. Code: initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier    ...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT