School of the Future Name:

Physics II Date:

Programming Exercise #3: Getting and Using Input

1. Open a new file in TextWrangler and save it as Ex3_UserInput.py in your folder on the Desktop. Then directly copy this script into that file.

2. Execute the program. (To do this, open Terminal and get into the folder on your Desktop.) You can do this by typing “python Ex3_UserInput.py” into the terminal.

a. If an error message pops up, that most likely means that there is at least one flaw in your program. See if you can identify where it is. Fix it and run the program again.

3. Look at what is printed. Does it make sense to you?

4. REFLECT:

a. Look at the first six lines of the code. A comment was added, but without the pound symbol. What sequence of character can be used to add multiple-line comments?

b. Look at line 25. What does the function time.sleep() do? Change the number in the parentheses of the function until you figure out what it does. Then google it to see if you’re right.

5. Now add the following lines of code to your file. Save it again, and re-run the program. If you encounter an error message, fix the issue and run the program again.

6. REFLECT: What is happening in Line 38? Be specific.

7. REFLECT: Reporting User Data

a. Rerun the program a few times, trying different numbers for the deposit (integers and decimals). What do you observe when you enter a decimal, such as 4503.67?

b. In Lines 27, 34, and 40, change %d to %f. Save the program and rerun it. How has what is printed changed?

c. In Lines 27, 34, and 40, change %f to %.2f. Save the program and rerun it. How has what is printed changed?

8. TROUBLE-SHOOTING WITH PYTHON:

a. Go back to your file in TextWrangler. Remove Line 9. Save the program and run it again. What error message do you get & why do you get it?

b. Go back to the file and fix Line 9. Now go to Line 19 and delete the second % symbol in that line. Save the program and rerun it. What error message do you get and why?.

c. Go back to the file and fix Line 19. Now Remove Line 32. Save the program and run it again. What error message do you get? (You don’t have to explain why yet.)

9. EXTENSION: Researching Common Data Types & Type Casting

a. There are many different data types that can be stored using variables. Below are the main ones we’ll be concerned with. Find out what each type is/means. Use Google or check out: http://www.tutorialspoint.com/python/python_numbers.htm

i. int

ii. float

iii. string (str)

b. When raw_input() is used and the user types something into the computer, it is saved as a string, regardless of what is typed. If we want to treat it like a number (in order to do addition, subtraction, etc.), it needs to be converted to an int or a float. This is sometimes called “casting”.

i. Find out how to convert a variable to an int or float. (Or how to “cast” a variable as an int or float.)

ii. What line in the program above did you do this? Try removing this line, saving the program, and running it again. What happens? Why does this occur?

NEVER DOUBT THAT A SMALL GROUP OF COMMITTED INDIVIDUALS CAN CHANGE THE WORLD, INDEED IT IS THE ONLY THING THAT EVER HAS. MEAD