In: Computer Science
Python
Pikachu is surrounded by pesky zubats! Will Pikachu be able to defeat all the bats, or will their numbers prove too great for our hero? You must write a program to answer this question. When your program starts, allow the user to input the number of zubats that are attacking Pikachu (you may assume the user enters a positive number). Then, you must simulate a series of battles between Pikachu and a zubat. Initially, Pikachu has 35 health, but he may lose health during the battles, and if he ever runs out of health, he faints! The zubats will attack one at a time, and the rules for the battle simulation are as follows. Note that you will definitely need to import Python’s random module, as there is luck in the simulation. • The zubat attacks rst. It has a 50% chance to hit Pikachu (Hint: Use the random() function to generate a random float between 0 and 1, then check if that oat is less than 0.5). If the zubat hits, it will randomly deal either 1, 2, or 3 damage to Pikachu. • Then Pikachu attacks. Pikachu has a 60% chance to hit the zubat. If he does, he will defeat the bat. The bats will attack in turn until either they are all defeated, or Pikachu faints. When that happens, print out a message that describes the outcome. • If Pikachu defeated all the zubats, print out that he triumped and how much health he has left • If Pikachu fainted, print out how many of the zubats he managed to defeat.
First, write a function to simulate a single battle between Pikachu and one zubat. Pikachu’s current health should be a parameter to this function, and the function should return the amount of health Pikachu has remaining after the battle is over. Make sure this function is working before going on! Put print() statements into your function to print out intermediate values during the battle to see if they make sense. You’ll take these print() statements out later (the sample output above doesn’t have them), but they’re invaluable for testing your function.
Second, in the “main” part of your Python program, use console input to ask the user for the number of zubats. You can assume the user will enter a positive integer. Then, use a loop to keep calling your function from Step 1 so long as there are still zubats to defeat and Pikachu hasn’t fainted. Once the loop is done, display the outcome to the console as per the sample output shown above.
CODE:
import random
def simulate_battle(health):
while True:
if random.random() < 0.5:
damage_done=random.randint(1,3)
health-=damage_done
if health<=0:
break
elif random.random() < 0.6:
break
return health
if __name__=="__main__":
zubats=int(input("Enter the number of zubats that are attacking
Pikachu..."))
pikachu_health=35
for i in range(zubats):
pikachu_health=simulate_battle(pikachu_health)
if pikachu_health<=0:
print("Pikachu has fainted....And the bats he managed to defeat is:
"+str(i))
break
if pikachu_health > 0:
print("All zubats died and Pikachu triumphed.....Remaining health
is: "+str(pikachu_health))
Sample of Observed Outputs:
1)
Enter the number of zubats that are attacking Pikachu...10
Pikachu has fainted....And the bats he managed to defeat is: 9
2)
Enter the number of zubats that are attacking Pikachu...10
All zubats died and Pikachu triumphed.....Remaining health is: 8
3)
Enter the number of zubats that are attacking Pikachu...15
Pikachu has fainted....And the bats he managed to defeat is: 11
Code Screenshot:
Screenshots of testing results by printing intermediate values: