+ 2

Intermediate Python - 27 Code Project - “Shooting Game”

Please help, thanks. You are creating a shooting game! The game has two types of enemies, aliens and monsters. You shoot the aliens using your laser, and monsters using your gun. Each hit decreases the lives of the enemies by 1. The given code declares a generic Enemy class, as well as the Alien and Monster classes, with their corresponding lives count. It also defines the hit() method for the Enemy class.

18th Jul 2021, 4:06 PM
Zach Z
8 odpowiedzi
+ 3
hi guys this is the solution. class Enemy: name = "" lives = 0 def __init__(self, name, lives): self.name = name self.lives = lives def hit(self): self.lives -= 1 if self.lives <= 0: print(self.name + ' killed') else: print(self.name + ' has '+ str(self.lives) + ' lives') class Monster(Enemy): def __init__(self): super().__init__('Monster', 3) class Alien(Enemy): def __init__(self): super().__init__('Alien', 5) m = Monster() a = Alien() while True: x = input() if x == 'exit': break elif x == 'laser': a.hit() elif x == 'gun': m.hit()
1st May 2022, 11:15 AM
Mustafa Al-jawdah
Mustafa Al-jawdah - avatar
+ 2
This code still not running elif x == 'laser': a.hit() elif x == 'gun': m.hit()
23rd Aug 2022, 10:26 PM
Credo Tengey
+ 2
Finally class Enemy: name = "" lives = 0 def __init__(self, name, lives): self.name = name self.lives = lives def hit(self): self.lives -= 1 if self.lives <= 0: print(self.name + ' killed') else: print(self.name + ' has '+ str(self.lives) + ' lives') class Monster(Enemy): def __init__(self): super().__init__('Monster', 3) def hit(self): super().hit() class Alien(Enemy): def __init__(self): super().__init__('Alien', 5) def hit(self): super().hit() m = Monster() a = Alien() while True: x = input() if x == 'exit': break elif x=='laser': a.hit() elif x=='gun': m.hit()
3rd Sep 2022, 11:32 PM
Credo Tengey
+ 1
You need to do the following to complete the program: 1. Inherit the Alien and Monster classes from the Enemy class. 2. Complete the while loop that continuously takes the weapon of choice from user input and call the corresponding object's hit() method. Sample Input laser laser gun exit Sample Output Alien has 4 lives Alien has 3 lives Monster has 2 lives
18th Jul 2021, 4:08 PM
Zach Z
0
class Enemy: name = "" lives = 0 def __init__(self, name, lives): self.name = name self.lives = lives def hit(self): self.lives -= 1 if self.lives <= 0: print(self.name + ' killed') else: print(self.name + ' has '+ str(self.lives) + ' lives') class Monster: def __init__(self): super().__init__('Monster', 3) class Alien: def __init__(self): super().__init__('Alien', 5) m = Monster() a = Alien() while True: x = input() if x == 'exit': break
18th Jul 2021, 4:08 PM
Zach Z
0
i'm stuck here also. waiting for help
13th Aug 2021, 3:01 AM
Tea & Coffee Husky
Tea & Coffee Husky - avatar
0
Stuck also
14th Feb 2022, 2:58 PM
Christopher Maganga
Christopher Maganga - avatar
0
The variable x is is taking input each time the while loop runs. So the first time the while loop runs x = "gun", and not ["gun", "laser", "laser", "laser", "exit"]. x is not an iterable. The second time he while loop runs x = "laser". The while loop will run indefinitely until x has "exit" entered as its input. For anyone who was having trouble with the While loop. I was trying to use a (for i in x) loop after the while loop to get things to work. This is what worked in the end for me. while True: x = input() if x == "exit": break elif x == "gun": m.hit() elif x == "laser": a.hit()
5th Feb 2023, 5:10 AM
phillip
phillip - avatar