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. 

(2) String Manipulation  (

...

3 points)

      Write a python program (myname.py - starter code in the repository) that uses a string of your full name, "the name game". 

...

Number of letters in your name is:  16

(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). 

(3b) A "for" loop

Again, copy the name program 3a from above to "username_name_loop.py". Modify  to allow the user to specify how many times they want to play "the name game".  Use a "for" loop to perform the operation whatever number of times the user requests.   

The logic should be:

  • Ask the user how many times they want to play. User provides n.
  • From that reply, make a for loop to do something n times.
  • Each time through the loop:
    • ask the user for a name
    • print the information about that name

Note, to do this efficiently should move your algorithm into a self-defined function so that you can just call it each time in the loop.  To make a function use this syntax:

def print_name(name):   #Defines a function called print_name that take a name as an input
     print(name)
     #count characters, make initials, print info to screen in this function.  

Then to call your function from the main 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))

(4) Push to GitHub

Add all of the ".py" files to your repository and push it to GitHub. 

Note: if you use "git add -A" it will add all of the files in your directory to the repository , so that you don't have to 'get add' each file separately.