2. ModulesAndScripts.pdf - 2 Modules Scripts and I\/O Topics Script Mode Modules The print and input statements Formatting First look at importing stuff

# 2. ModulesAndScripts.pdf - 2 Modules Scripts and I/O Topics...

This preview shows page 1 - 13 out of 46 pages.

2. Modules, Scripts, and I/O Topics: Script Mode Modules The print and input statements Formatting First look at importing stuff from other modules The Windchill Calculation Let’s compute the windchill temperature given that the air temperature is T = 32F and the wind is W = 20mph. Here is the formula courtesy of the National Weather Service: The formula only applies if T <= 50F and W>=3mph. 16 . ) 4275 . 0 75 . 35 ( ) 6215 . 0 74 . 35 ( W T T W chill Use Python in Interactive Mode >>> Temp = 32 >>> Wind = 20 >>> A = 35.74 >>> B = .6215 >>> C = -35.75 >>> D = .4275 >>> e = .16 >>> WC = (A+B*Temp)+(C+D*Temp)*Wind**e >>> print WC 19.9855841878 The print statement is used for displaying values in variables. Quick Note on print The line >>> print WC results in the display of the value currently housed in the variable WC More on the print statement later. Motivating “Script Mode” What is the new windchill if the wind is increased from 20mph to 30mph? Looks like we have to type in the same sequence of statements. Tedious. Wouldn’t it be nice if we could store the sequence of statements in a file and then have Python “run the file” after we changed Wind = 20 to Wind = 30 ? Script Mode Instead of running Python in interactive mode , we run Python in script mode . The code to be run (called a script ) is entered into a file (called a module ). We then ask Python to “run the script”. What is a Module? A module is a . py file that contains Python code. In CS 1110, these are created using Komodo Edit. The Module WindChill.py Temp = 32 Wind = 20 A = 35.74 B = .6215 C = -35.74 D = .4275 e = .16 WC = (A+B*Temp)+(C+D*Temp)*Wind**e print WC Produced using Komodo Edit. This is our first draft. WindChill.py Running the Module Here are the steps to follow in the command shell: 1. Navigate the file system so that you are “in” the same diretory that houses WindChill.py 2. Type: python WindChill.py Details Suppose I have a directory on my desktop called TODAY where I keep all my python files for today’s lecture. I navigate the file system until I get this prompt: C:\Users\cv\Desktop\TODAY> Asking Python to Run the Code in WindChill.py C:\Users\cv\Desktop\TODAY> Python WindChill.py 19.6975841877955 To save space in subsequent slides, we will refer to C:\Users\cv\Desktop\TODAY> as Blah\Blah> Multiple Statements on a Line Temp = 32 Wind = 20 A=35.74;B=.6215;C=-35.74;D=.4275;e=.16 WC = (A+B*Temp)+(C+D*Temp)*Wind**e print WC Can put multiple statements on a line. Separate the statements with semicolons.  #### You've reached the end of your free preview.

Want to read all 46 pages?

• Fall '19
• Irrational number, Wind chill
• • •  