Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This program needs to be written in Python 3. It is for a first year computer science class and we have learned up to loops
This program needs to be written in Python 3. It is for a first year computer science class and we have learned up to loops and conditionals, but not lists or sorting.
Question 3 (9 points): Purpose: To practice combining conditionals and loops Degree of Difficulty: Tricky. You'll need to use everything you've learned so far. There are some problems in science and mathematics in which a particular process can be described simply and precisely, but actually doing the math to solve it perfectly is very tedious (and for large versions of the problem. perhaps impossibly tedious). One powerful tool we can use in such cases is simulation, where we simulate the process itself many, many times and just count the results. This is especially useful when the process involves randomness. You'll practice writing such a simulation for this question. The simulation A Koffing is on the loose, and is about to poison the city with its noxious gas! Only Pikachu can save the day. Pikachu will have to defeat the Koffing in a single blow 1 before the Koffing can release its fumes. The Koffing has a fixed amount of health, and we can simulate Pikachu's attack by rolling a bunch of 6- sided dice. In Python, we'll "roll" a die by importing the random module and using the randint() method to generate a random number from 1 to 6. Here are the rules for the dice roll: Each 6 rolled on the dice deals 1 damage Rolling a 5 on the dice does nothing by itself. But every PAIR of 5s will deal 4 damage (it's super effective!) . HOWEVER, 1 is an unlucky number. If three or more ls are rolled, the ENTIRE ATTACK is cancelled and deals zero damage. If the total damage from the roll equals or exceeds the Koffing's health, then it is defeated and Pikachu has saved the city. Example 1: The Koffing has 3 health. Pikachu rolls 6 dice and gets (1. 2. 3. 4. 5. 6). There is only one 6. so the total damage is 1, which is not enough to defeat the Koffing . Example 2: The Koffing has 3 health. Pikachu rolls 6 dice and gets (1, 2, 3, 1.5, 5). There are no 6s, but there are two 5s, which together deal 4 damage. The Koffing is defeated! Example 3: The Koffing has 9 health. Pikachu rolls 6 dice and gets (5.5.5. 5. 5. 6). There are TWO pairs of 5s, which together deal 8 damage (the extra 5 is of no value), and then one 6 to deal 1 more damage. This makes for 9 damage total, so the Koffing is defeated! . Example 4: The Koffing has 3 health. Pikachu rolls 6 dice and gets (1. 1. 1. 6.6.6). There are at least three 1s. so the entire attack deals no damage and the Koffing is not defeated. The goal for your simulation is to determine how likely it is that the Koffing will be defeated with a given amount of health and dice. You'll do this by simulating the procedure outlined above 1000 separate times and counting the results. Conceptually, it's no different than if you just physically rolled the dice yourself, wrote down whether or not the Koffing was defeated, and then repeated that process another 999 times. Sample Run Sample input and output (input typed by the user is shown in green text): How many dice does Pikachu have? 6 How much health does Koffing have? 2 Pikachu rolled 6 dice against a Koffing with 2 health. Pikachu won 470 out of 1000 times ( 47.0 %) Don't worry, this is Pokemon. The Koffing isn't hurt; it merely faints Program Design To get started, you should divide your work into two parts. Step 1 First, write a function to carry out a single attack by Pikachu. The number of dice to use and the health of the Koffing should be parameters to the function, and the function should return True if the Koffing is defeated and False if it is not. Make sure this function is working before going on! Put print() statements into your function to print out exactly the numbers that Pikachu rolls on the dice along with the function's result (True or False) so that you can validate it yourself. You'll take these print() statements out later (the sample output above doesn't have them), but they're invaluable for testing your function. Step 2 Second, in the "main" part of your Python program, use console input to ask the user for the number of dice and the health of the Koffing. You can assume the user will enter positive integers. Then, use a loop to call your function from Step 1 1000 times and keep track of the results. Once the loop is done, display the results to the console as per the sample output shown above. Demonstrating your program For this question, you will hand in some testing. Copy and paste the console output of your program into a separate document for the following test cases. . Case 1: 6 dice and 2 health (i.e. the sample run shown above) Case 2: 8 dice and 3 health . Case 3: 10 dice and 5 health Also in your testing document, write a short answer (one or two sentences is enough) to the following question: assuming Koffing's health stays the same, is adding more dice ALWAYS better for Pikachu? Why or why not? What to Hand In A file called a3q3.py containing your finished program, as described above. A document called a3q3.txt (rtf. .pdf and doc are also okay) containing the console output from the cases above, along with your short answer to the written question. Label your output to make it more legibleStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started