Question

In: Computer Science

Python Pikachu is surrounded by pesky zubats! Will Pikachu be able to defeat all the bats,...

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.

Solutions

Expert Solution

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:


Related Solutions

Write the following Python script: Pikachu is a well-known character in the Pokemon anime series. Pikachu...
Write the following Python script: Pikachu is a well-known character in the Pokemon anime series. Pikachu can speak, but only 3 syllables: "pi", "ka", and "chu". Therefore Pikachu can only pronounce strings that can be created as a concatenation of one or more syllables he can pronounce. For example, he can pronounce the words "pikapi" and "pikachu". You are given a String word. Your task is to check whether Pikachu can pronounce the string. If the string can be produced...
Although bats are not known for their eyesight, they are able to locate prey (mainly insects)...
Although bats are not known for their eyesight, they are able to locate prey (mainly insects) by emitting high-pitched sounds and listening for echoes. A paper gave the following distances (in centimeters) at which a bat first detected a nearby insect. 61 24 52 56 42 34 27 42 68 45 83 (a) Compute the sample mean distance at which the bat first detects an insect. (Round your answer to three decimal places.) ? cm (b) Compute the sample variance...
Although bats are not known for their eyesight, they are able to locate prey (mainly insects)...
Although bats are not known for their eyesight, they are able to locate prey (mainly insects) by emitting high-pitched sounds and listening for echoes. A paper gave the following distances (in centimeters) at which a bat first detected a nearby insect. 23 40 27 56 52 34 42 61 68 45 83 (a) Compute the sample mean distance at which the bat first detects an insect. (Round your answer to three decimal places.) cm (b) Compute the sample variance and...
On May 28, 2021, Pesky Corporation acquired all of the outstanding common stock of Harman, Inc.,...
On May 28, 2021, Pesky Corporation acquired all of the outstanding common stock of Harman, Inc., for $510 million. The fair value of Harman's identifiable tangible and intangible assets totaled $575 million, and the fair value of liabilities assumed by Pesky was $149 million. Pesky performed a goodwill impairment test at the end of its fiscal year ended December 31, 2021. Management has provided the following information: Fair value of Harman, Inc. $ 490 million Fair value of Harman's net...
Why is it important to be able to be able to count all of the ways...
Why is it important to be able to be able to count all of the ways something can occur?
Java or python. The clients need to be able to communicate with each other using the...
Java or python. The clients need to be able to communicate with each other using the command line. The "local chat" program should be implemented using a client/Server model based on UDP protocol. Requirements: 1. The chat is performed between 2 clients and not the server. 2. The server will first start up and choose a port number. Then the server prints out its IP address and port number. 3. The client then will start up and create a socket...
LOOK over code Python The task aims to develop a Kalman filter that is able to...
LOOK over code Python The task aims to develop a Kalman filter that is able to hit the moving target (the pink box) in as many situations as possible. However, there are some limitations: IT IS NOT PERMITTED TO CHANGE THE PRODEIL AND TARGET CODE IT IS ALSO NOT ALLOWED TO CHANGE THE GAME LOOP, OTHER THAN WHAT HAS BEEN COMMENTS SHALL BE CHANGED WHEN THE Kalman CLASS IS IMPLEMENTED I have made the callman class, but my question is;...
Delta Corp has 2 equal shareholders. Able and Body. Delta and Able & Body are all...
Delta Corp has 2 equal shareholders. Able and Body. Delta and Able & Body are all cash method calendar year taxpayers. On 1/01 of the current year, Delta’s A/E&P was $5,000, and its C/E&P during the present year was $20,000. Each of the E&P figures were computed without any adjustment needed because of the distributions described below. On 7/01 of the present year, Delta distributed some machinery to Able and some equipment to Body. The machinery distributed to Able had...
Lexington Company produces baseball bats and cricket paddles. It has two departments that process all products....
Lexington Company produces baseball bats and cricket paddles. It has two departments that process all products. During July, the beginning work in process in the cutting department was half completed as to conversion, and complete as to direct materials. The beginning inventory included $40,000 for materials and $60,000 for conversion costs. Ending work-in-process inventory in the cutting department was 40% complete. Direct materials are added at the beginning of the process. Beginning work in process in the finishing department was...
Lexington Company produces baseball bats and cricket paddles. It has two departments that process all products....
Lexington Company produces baseball bats and cricket paddles. It has two departments that process all products. During July, the beginning work in process in the cutting department was half completed as to conversion, and complete as to direct materials. The beginning inventory included $40,000 for materials and $60,000 for conversion costs. Ending work-in-process inventory in the cutting department was 40% complete. Direct materials are added at the beginning of the process. Beginning work in process in the finishing department was...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT