You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This is your 1st homework assignment that requires coding!  Please read ALL the instructions carefully, especially those on the main homework page.

Read the whole assignment before you begin.


IMPORTANT:  For this assignment I want you to write program (A .py python code file that can be run at the command line).  No JupyterLab Notebooks for this HW!

(0) Completion of Labs and Reading

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

(1)  Checkout the HW03 repository:

Login to Rivanna

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

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  (4 points)

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


For me, that would by

MYNAME="Robert Craig Group".


Split this string into words, get the initials, and count how many letters there are.  Depending on how many names you have (I have three - use the "len" function to count yours), have your program print out each name.  Your program should work for names with up to 4.  You will need to use the "if or else/if" conditional to only print the names that were given if less and 4.  


Your program output should look like this:


Your first name is: Robert

Your second name is: Craig

Your third name is: Group

Your initials are: R.C.G.

Number of letters in your name is:  16

(3) Handling user input  (6 points)

(3a) 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 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:


print_name(my_name)  #If your string is called my_name



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

  • No labels