You may need to refer to this week's reading assignment throughout the exercises.
Announcements
Your are doing great.
- The class will pick up speed now. Make sure to keep up!
User Input
Find a partner - or PG will do it for you. Don't delete your programs from the class. It is important to have familiar and working code examples to refer to as you write new programs! Also, while we do not grade the in-class work rigorously, your participation is part of your grade. So, make sure you post the required files to Collab (so that we know you were present and working).
Remember to log into rivanna to begin!
Important: Refer to the getting_started page for instructions on connecting to your rivanna account.
Use this link to create your repository for class07 on GitHub:
As we learned how to do in class02, clone the class07 repository into your working area on Rivanna.
>>> git clone git@github.com:PHYS1655S24/class07-<userid>.git
Navigate into that directory via usage of the 'cd' command.
Goals
- Handle user input
- Learn how to import modules
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.
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 them. It 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 the first exercise, we used the input function to allow the user to input a character to your program. 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.
Do that by including this line at the top of your program - we can just include the functionality that we need today - argv:
>>> from sys import argv
The sys module provides 'argv', which is a 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!)
NOTE: You do not need the "sys." since we imported argv directly,
If you want to check the first argument passed by the user on the command line:
>>> arg1=argv[1] #(python starts counting at 0!)
For example, if the user ran program.py with one input parameter:
>>> python program.py par1
We could get access to "par1" inside of program.py by accessing sys.argv[1]. "argv" is a Python list, its length is 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 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 .