Versions Compared

Key

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

...

Use this link to create your repository for class07 on GitHub: https://classroom.github.com/a/ZGFjjIy2 


As we learned how to do in class02, clone the class07 repository into your working area on Rivanna. 

...

NOTE:  For today's work you actually need to write a .py file (python program).  Do not try to use JupyterLab, because some of the exercises do not make sense in that environment

1) Fun with strings. (if time remains- Prof. Group -make sure input.py is in class5 repo and that you talk some about strings in the slides)


Copy over input.py to a file named string_fun.py. Make sure to update the header information at the top of the file.


In this program request an input string from the user (hint: use the input function).  Your program should:

  • Check how many words are in the string (use strip and split - check documentation for how to do it), and print info to the screen.
    • Count how many characters are in the string, and print the info to the screen.
    • print the first 2 characters to the screen.
    • print the last 2 characters to the screen.
  • Request an integer from the user (use input).  Check to make sure it is an integer.  Hint: use try/except (Google it if you need to) to make sure.  Input validation! 
  • Use a for loop (example in reading) to print the string to the screen the number of times of the integer that they input can count themIt should look like this:

1 : this is my string

2 : this is my string

3 : this is my string

4 : this is my string

5 : this is my string


Include documentation/comments to string_fun.py and add, commit, and push it to your repository on GitHub . . 


2) User input at the command line. 


  In Class04 the first exercise, we used the " input " function to allow the user to input a character to your program.  Today we are going to Now, we will learn how to run a Python program with a command line argument. Run the start code "input.py" that is included in your repository, and take a look at the code to see how it works. 


  Open input.py in your favorite editor.  Use emacs if you don't have a favorite yet.  Make sure to update the header information at the top of the file.


  Today we will use the functionality that comes with the "sys" module.  To include modules we must "import" them. 

...

  The sys module provides 'argv', which is a python Python list containing the following strings:  program name, command line arg1, command line arg2, ..., command line argN

...

So, if you want to access the name of the program, from within your program:


>>>  pname=argv[0]  # program name is the first list item in argv (python starts counting at 0!)

...

>>> python program.py par1


We could get accesss access to "par1" inside of program.py by accessing sys.argv[1].  "argv" is a Python list, its length is al at least 1, but can be longer depending on if extra **command-line arguments** are passed in when you run your program!

...

Add the functionality to 'input.py' to accept one command-line-argument from the user, and print it to the screen.

...


3) input validation


  What happens if your user doesn't input anything?  What happens if your user enters more than one command line argument?  A good program includes checks to make sure that input is valid.   



You should always include input validation as needed in your programs.  Modify input.py to include some validation.  It would be great if you came up with your own.  But, you can basically copy what I have above (if you understand it).  Test that everything works as expected.  We will learn methods later in the course to check the type of data included.   Include documentation to input.py and add, commit, and push it to your repository on GitHub . 

...