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,...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
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
(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
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...
Python I am creating a class in python. Here is my code below: import csv import...
Python I am creating a class in python. Here is my code below: import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(obj.POP(line)) population.append(obj.POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip() The problem is, I get an error saying:  ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT