Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • If you did not yet complete the in-class work or the weekly reading, then you may want to finish that first. 

...

Use this link to accept the assignment and create your repository on GitHub:   https://classroom.github.com/a/hrIKgSCLh7qCeV9S

After you accept the assignment and the repository and it exists in your GitHub, as we did in class02, clone the repository into your working area on Rivanna. 

...

(3) Handling user input  (3 points)

...

User input with the input function. 

Copy the name program from above to "name_input.py".  Rather than use a string defined in the program, this version should request the users user's name, and then print the output for the name that is given.  Hint:  use the built-in "input" function.  Make sure that your program works for names with up to 4 sub-names.  Think about what input validation is necessary so that your program won't crash if a user gives poor input?    We will discuss input validation next week, so for now,  just explain what you think is important (in a comment at the top of your program)  Implement error handling with the "try/except" technique discussed in class.  


(2) Calculate π  Using Monte Carlo Integration (4 points)

PI can be easily calculated using Monte Carlo techniques. Write a program to do this called mc_pi.py.  There is starter code in the repository.  Your starter code allows the user to input how many random coordinates (N_MC) to throw at the command line  (remember sys.argv?).  Do you understand the starter code?  What happens if you put in bad data?  


Let's say that you know that the area of a circle is πr2. Assume a unit circle centered in a square of area 4 units. Generate N_MC uniform random pairs of numbers inside the area of the square for the x and y coordinates, and check to see if the point is inside of the circle (x2+y2<1). Hint: be careful with where you place your origin and how you calculate the radial distance from the center of the circle.  By counting how many points fall inside of the circle you can estimate the area of the circle.  From that value you can calculate pi.

Try 10,000 random points. How close are you to the true value of π given by the Python math module (import math, math.pi)?  Compare in your program and print the comparison to the screen. 

Use the time module to measure how long your program took to run. That is, in your program use code something like this:


import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

...