Run update21 to create the cs21/labs/06 directory. Then cd into your cs21/labs/06 directory and create the python program for lab 6 in this directory (handin21 looks for your lab 6 assignment in your cs21/labs/06 directory):
$ update21 $ cd cs21/labs/06In your cs21/lab/06 directory, you will create a single python program, halfLife.py, that will contain your solution for lab 6. Note: this lab is to be completed in one file, halfLife.py. Please complete all subtasks listed below as part of a larger program. Be sure to read through the whole lab document before starting, and you can run handin21 multiple times to turn in the portion of the program you have completed at any point in time.
Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.
The goal of this assignment is to practice using while loops as well as creating and using functions. Your program, at a high level, will simulate radioactive decay of atoms according to a substance's half life. A half life is the amount of time it takes for a substance undergoing decay to decrease by half. It is a very important concept in the sciences. Archaeologists, for example, use the half-life of carbon-14 to calculate the age of fossils precisely (known as carbon dating).
Your program will ask the user for the number of atoms in the starting population and the half-life period and simulate the decay of atoms one half-life at a time until there are no more atoms left. Follow the steps below to create your program.
Open a file named halfLife.py and write a function getPositiveInt(message) which returns a positive integer from user input. getPositiveInt has the following parameter:
If the user enters a negative number, your function should print an error message and then reprint the prompt instructions. getPositiveInt should print the instructions until the user enters a positive number.
If the user enters a real number, your function should convert it to an int. Remember, to convert a floating point value to an int, use the int() function. For example, x = int(8.7) converts the float value 8.7 to the int value 8 (i.e., it truncates 8.7), assigning x the int value of 8.
Once you have your getPositiveInt function implemented, test it by making calls to it from the main function. Here is a sample result where main invokes getPositiveInt with the argument "How many apples do you have? "
How many apples do you have? -3 That is not a positive integer! Try again. How many apples do you have? 0 This is not a positive integer! Try again. How many apples do you have? 3Here, the function should return the value 3. Another sample run with the argument "Enter number of atoms: "
Enter number of atoms: 100.5
Here, the function returns 100.
Write a function oneHalfLife(pop_size, time) that simulates one half life. This function has two parameters:
oneHalfLife should perform 3 actions:
Write a function, decayToZero(start_size, time), which simulates a population of atoms decaying to 0 atoms. This function has two parameters:
Write a main program using the functions defined above to simulate the decay of a population of atoms. Your main function should satisfy the following requirements:
$ python halfLife.py This program simulates the decay of radioactive atoms Enter number of atoms: -9 That is not a positive integer! Try again. Enter number of atoms: 100 Enter length of half-life in seconds: 0.2 The initial population size is: 100 There are 50 atoms left There are 25 atoms left There are 12 atoms left There are 6 atoms left There are 3 atoms left There are 1 atoms left There are 0 atoms left The atoms lasted 7 generations for a total of 1.4 secondsHave fun and test out different radioactive substances. Krypton-81m has a half life of 13 seconds; lithium-8 is only 0.838 seconds. I don't suggest trying carbon-14. With a half-life of 5715 years, you might be waiting a long time for your results!
Once you are satisfied with your program, hand it in by typing
handin21 in a terminal window.