- 4

How to put While into IF-Else (text base game in Python)

Hello! I'm rookie in Python, and starting to learn by this fun example. Can't understand how to make 2 decisions: 1- you start shooting, and While is operating, 2- just print the letter On PC i can make it with wait input(), but after chose of 2, 1-st message is printing anyway. I think i need some function.. Thx for help! print ("While walking on your yard, you saw some strange moves in the grass\nIt was alien, ugly and horrible.\nNext moment you already took the lazer gun\nand you started to think:\n 1) Of course I can kill him with my lovely gun \n 2) Hmm, maybe I should wait, he is just ugly :) ") print ("*******") a = int(input()) if a == 1: print ("You chose to start shooting.\nBattle has begun") print ("*******") else: print ("It was clever idea, monster has teleported to his ship \nand flew away in deep space") alien = 5 lazer = 1 action = True while action: alien = alien - 1 print ("You hit alien! HP left:") print (alien) if alien == 1: print ("I got him hard") continue if alien <= 0: print ("Last critical hit moved out alien's brain and his soft body laid down on the ground") break

22nd Jun 2018, 10:35 AM
Evgeny Savkin
Evgeny Savkin - avatar
4 Answers
+ 1
Please give the proper code of this.
5th May 2021, 12:19 PM
Avanti Bhamare
Avanti Bhamare - avatar
+ 1
# Here is the code for the shooting Game 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: #krish 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()
27th May 2021, 1:34 PM
R Krishnan Vijay
R Krishnan Vijay - avatar
if i==1: while x==1: print(“988”) else: print(“gailed”) is this what you want?
22nd Jun 2018, 9:22 PM
🅿️®️⭕️_e✖️e - avatar
- 1
No, "while action:" part is working then user uses 2 way ( else : part ). But i need 2 ways , 1 way - fight is starting with while function, 2 way - python just prints me else: part
25th Jun 2018, 1:57 PM
Evgeny Savkin
Evgeny Savkin - avatar