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

In python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
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: I want to make the following code to prompt the user if want to run...
Python: I want to make the following code to prompt the user if want to run the software again. I am new to python, so i do not know if it works like c++. If can explain that would be much appreciated base = float(input("Enter base of the triagle: ")) Triangle_Right = float(input("Enter right side of the triagle: ")) Triangle_Left = float(input("Enter left side of the triagle: ")) height = float(input("Enter the height of the triangle: ")) perimiter = base...
I know how to do this with arrays, but I have trouble moving my code to...
I know how to do this with arrays, but I have trouble moving my code to use with linked lists Write a C program that will deal with reservations for a single night in a hotel with 3 rooms, numbered 1 to 3. It must use an infinite loop to read commands from the keyboard and quit the program (return) when a quit command is entered. Use a switch statement to choose the code to execute for a valid command....
(Python) How would I add this input into my code? "Enter Y for Yes or N...
(Python) How would I add this input into my code? "Enter Y for Yes or N for No:" as a loop. def getMat(): mat = np.zeros((3, 3)) print("Enter your first 3 by 3 matrix:") for row in range(3): li = [int(x) for x in input().split()] mat[row,0] = li[0] mat[row,1] = li[1] mat[row,2] = li[2] print("Your first 3 by 3 matrix is :") for i in range(3): for k in range(3): print(str(int(mat[i][k]))+" ",end="") print() return mat def checkEntry(inputValue): try: float(inputValue) except...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT