Loops can be nested in Python, as they can with other programming languages. A nested loop is a loop that occurs within another loop, structurally similar to nested if statements.
Pikachu is surrounded by pesky Gengars! Will Pikachu be able to defeat all the ghosts, or will their numbers prove too great for our hero? You must write a program to answer this question.
When the program starts, it will allow the user to input the number of Gengars that are attacking Pikachu.
Then, a series of battles between Pikachu and a Gengar will commence. Initially, Pikachu has 35 health, but he may lose health during the battles, and if he ever runs out of health, he faints!
The Gengars will attack one at a time, and the rules for the battle simulation are as follows.
- The Gengar attacks first. It has a 50% chance to hit Pikachu. If the Gengar hits, it will randomly deal either 1, 2, or 3 damage to Pikachu.
- Then Pikachu attacks. Pikachu has a 60% chance to hit the Gengar. If he does, he will defeat the ghost.
The ghosts will attack in turn until either they are all defeated, or Pikachu faints. When that happens, a message will be printed out that describes the outcome.
- If Pikachu defeated all the Gengars, print out that he triumphed and how much health he has left
- If Pikachu fainted, print out how many of the Gengars he managed to defeat.
Here are the steps for how to open, use and utilize the program:
- First, download all of the files listed above;
- Put all the files in one folder;
- Open the file Projec_pikachu_gengar.py;
- The program will open a command console which will ask you to input the number of Gengars.