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

Compare with Current View Page History

Version 1 Next »

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: https://classroom.github.com/a/ZGFjjIy2


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) User input at the command line. 


  In Class04 we used the "input" function to allow the user to input a character to your program.  Today we are going to 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 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]  #(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 accesss to "par1" inside of program.py by accessing sys.argv[1].  "argv" is a Python list, its length is al 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.


2) 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 type of data included.   Include documentation to input.py and add, commit, and push it to your repository on GitHub . 




  • No labels