Lesson 4: Who Goes There?

Similar documents
Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Starting. Read: Chapter 1, Appendix B from textbook.

ASCII Art. Introduction: Python

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Create your first workbook

BEGINNER PHP Table of Contents

The Domino Designer QuickStart Tutorial

Lesson 18: Animation. Computer Programming is Fun!

Writing and Running Programs

SHOUTcast v2 - Broadcasting with Nicecast

Starting SAS. 2. Click START, ALL PROGRAMS, SAS, and the SAS ICON

NCMail: Microsoft Outlook User s Guide

Excel VBA. Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data.

Getting Started with Python and the PyCharm IDE

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

Laboratory Exercise #0

Outlook - an Introduction to Version 2003 Table of Contents

Tracking changes in Word 2007 Table of Contents

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Part 1: Understanding Windows XP Basics

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

NCMail: Microsoft Outlook User s Guide

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

B - Broken Track Page 1 of 8

Post Experiment Interview Questions

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

This lesson is part 5 of 5 in a series. You can go to Invoice, Part 1: Free Shipping if you'd like to start from the beginning.

MITOCW watch?v=0jljzrnhwoi

Lesson 3 Transcript: Part 2 of 2 Tools & Scripting

C# Programming Tutorial Lesson 1: Introduction to Programming

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Microsoft Office Word 2010

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Lesson 1: Writing Your First JavaScript

Here are all the comments that your Udacity Code Reviewer had about your code...

Intro. Scheme Basics. scm> 5 5. scm>

The Turing Environment

Chapter One: Getting Started With IBM SPSS for Windows

District 5910 Website Quick Start Manual Let s Roll Rotarians!

Using Windows 7 Explorer By Len Nasman, Bristol Village Computer Club

Getting Started with AnyBook

Open Book Format.docx. Headers and Footers. Microsoft Word Part 3 Office 2016

Taskbar: Working with Several Windows at Once

Getting help with Edline 2. Edline basics 3. Displaying a class picture and description 6. Using the News box 7. Using the Calendar box 9

Formatting Page Numbers for your Thesis/ Dissertation Using Microsoft Word 2013

Using X-Particles with Team Render

Introduction. Basic Troubleshooting Tips. Computer Basics What are some troubleshooting techniques? What are Some Troubleshooting Techniques?

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Lesson 1 using Dreamweaver CS3. To get started on your web page select the link below and copy (Save Picture As) the images to your image folder.

Arduino IDE Friday, 26 October 2018

Instruction manual Gulden desktop wallet 1.6.1

The PCC CIS etutorial to Outlook

Barchard Introduction to SPSS Marks

Using web-based

Copyright 2004, Mighty Computer Services

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

The smarter, faster guide to Microsoft Outlook

Heads Up Design: Focusing on the drawing area

Why You Should Not Use Arch

Outlook Web Access. In the next step, enter your address and password to gain access to your Outlook Web Access account.

User Accounts. The Passwd, Group, and Shadow Files

How To Set User Account Password In Windows 7 From Guest

Subversion was not there a minute ago. Then I went through a couple of menus and eventually it showed up. Why is it there sometimes and sometimes not?

KMyMoney Transaction Matcher

New to the Mac? Then start with this lesson to learn the basics.

The following content is provided under a Creative Commons license. Your support

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Get to know Word 2007 I: Create your first document Quick Reference Card

A Document Created By Lisa Diner Table of Contents Western Quebec School Board October, 2007

How to Use. Working for you. By the abc27 News Team. abc27, WHTM-TV 3235 Hoffman Street Harrisburg, PA

Exploring UNIX: Session 3

COPYRIGHTED MATERIAL. Dipping Your Toe into Python. Part I. Chapter 1: Programming Basics and Strings. Chapter 2: Numbers and Operators

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Flowcharts for Picaxe BASIC

A computer program is a set of instructions that causes a computer to perform some kind of action. It isn t the physical parts of a computer like the

CSI Lab 02. Tuesday, January 21st

Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide

Section 2. Sending s

Here we will look at some methods for checking data simply using JOSM. Some of the questions we are asking about our data are:

Office365 Outlook. The username is your complete employee number followed

Before You Lose Your iphone

Microsoft's latest OS is a lot better than its predecessor, but it still has some annoying quirks. We help you solve them.

NSCC SUMMER LEARNING SESSIONS MICROSOFT OFFICE SESSION

Lesson 1. Importing and Organizing Footage using Premiere Pro CS3- CS5

Here is a step-by-step guide to creating a custom toolbar with text

ICDL & OOo BASE. Module Five. Databases

Section 1. System Technologies and Implications. Modules

Introduction to Microsoft Office 2007

MITOCW watch?v=9h6muyzjms0

You ll find everything you need to get started with your VaaS-t conferencing account in the following pages.

Bucknell University Digital Collections. LUNA Insight User Guide February 2006

Earthwork 3D for Dummies Doing a digitized dirt takeoff calculation the swift and easy way

Lesson 2. Introducing Apps. In this lesson, you ll unlock the true power of your computer by learning to use apps!

Here we will look at some methods for checking data simply using JOSM. Some of the questions we are asking about our data are:

Get comfortable using computers

Transcription:

Lesson 4: Who Goes There? In this lesson we will write a program that asks for your name and a password, and prints a secret message if you give the right password. While doing this we will learn: 1. What a function is and how to call it. 2. How to set the value of a variable to something that the user typed (input) Let's begin creating the password program. Start IDLE. Create a new Editor window by selecting the File menu in the Python Shell window, then selecting New Window. Type the following line in the new Editor window: print Hello Now let's save the file before we go any further. Let's call this program password.py. Select the File menu, then Save. In the File name box type password.py. Save the file in your MyPrograms directory. Please review The Editor Window on page 7 for instructions on how to save a file in your MyPrograms directory. So far this is just the same as our hello.py program, but we will add more commands in a moment. Just for fun, press the F5 key to run this program. Python says: Hello! in the Python Shell window. In the password.py window, put the cursor at the beginning of the first line, before any other characters, and press Enter. This creates a new blank line at the beginning of the file. Type the following in the blank line you just created: name = raw_input("who are you? ") Now find the line that looks like this: print "Hello!" and make it look like this: print "Hello", name Do you notice that there is an asterisk (*) before and after the file name in the title bar of the hello.py window? That means that we have made changes to the file but have not yet saved the changes. You can't run a new or changed program until it has first been saved. Press Ctrl-S to save the file. Ctrl-S means press and hold down the Ctrl key ( control ), press the S key, then release the Ctrl key. Now press the F5 key to run the program. The Python Shell window should come to the front, and you will see a prompt from your very own program, asking you: Who are you? This is a question that we should all ask ourselves. What do you think will happen when you type your name and press Enter? Try it. 21

Hello David FYI: If you typed your name but you didn't see anything, or it appeared in the wrong window, try clicking your mouse on the title bar of the Python Shell window first, so it gets the focus. When you see the prompt again in the Python Shell window you know that your program has finished running. About Functions Let's look at the password.py window again. Consider the command that you used to print a prompt and get input from the user: name = raw_input("who are you? ") In the line above, the word raw_input is the name of a function that comes built-in to Python. Whenever you have a Python name followed immediately by a pair of parenthesis (), you are calling a function. A function is a piece of a program that you can run again and again in your own program to accomplish some specific task. In this case, raw_input() is a built-in function that gets input from the user. It prints a prompt and waits for the user to type something and press Enter. The function returns what the user typed. The string inside the parenthesis is called the parameter to the function. It gives the function some information it needs to do its work. In this case the parameter is the prompt Who are you?. raw_input() will print its prompt parameter before it waits for input for the user. After the user has typed something and pressed Enter, raw_input() returns a value. In this case the value is a string containing the text that the user typed. If you put the raw_input() command on the right-hand side of an = and there is a Python name on the left hand side, then the returned value is assigned to a variable. Functions are a very useful concept. I will show you later in this book how to write your own functions. More on the raw_input() Function FYI: Why do they call it raw_input? Is there a cooked input? Yes, sort of. There is an input() function, but you don't want to use it very often. It takes the input and immediately executes it as a Python expression (command that results in a value.) That's probably not what you want, as you will often get errors. Before we go on, let's practice calling the raw_input() function with different parameters. In the Python Shell window try the following: raw_input("what is your favorite color? ") What is your favorite color? green 'green' Notice that I put a space after the? and before the ending quote mark in the parameter. Otherwise it would look like this: raw_input("what is your favorite color?") What is your favorite color?green 22

'green' Now try a different parameter. When Python asks you how many lumps you want, just press Enter (because you don't want any lumps.) raw_input("how many lumps would you like? ") How many lumps would you like? '' (When it printed '' that meant that raw_input() returned an empty string.) Now try raw_input() without any prompt parameter. On the blank line after the raw_input() command type test. raw_input() test 'test' The Python Shell printed the return value for you. Notice that the returned string values have single quotes around them, like this: 'test'. Python will accept double quotes or single quotes around strings, as long as you begin and end the string with the same kind of quote mark. That is one way that you can put quote marks inside of a string. Try this: print 'Did someone say "ice cream"?' Did someone say "ice cream"? Now let's go back to the password.py window and turn this program into something more interesting. (If you closed the password.py window, then open your password.py file again.) Let's pretend that you have a secret that you want to share with everyone in the house but your sister (it's a good secret, a birthday present or something like that.) Let's assume that no one but you knows how to read and edit a Python program (that won't last for long if they read this book.) We'll put a secret message into this program that only someone with the right password can access. Go to the end of the file and press Enter to insert a blank line. (Python doesn't mind blank lines.) Add the following lines to the end of the program: message = "Sally is getting a bicycle for her birthday." password = raw_input("what is the password? ") if password == "birthday": else: print message print "Wrong password." raw_input("press Enter to finish.") Press Ctrl-S to save the program and F5 to run the program. If you typed everything correctly, you should see something like this in the Python Shell window: Who are you? David 23

Hello David What is the password? birthday Sally is getting a bicycle for her birthday. Press Enter to finish. Go back to the password.py window and press F5 again. If you use the wrong password it should look like this: Who are you? Sally Hello Sally What is the password? horse Wrong password. Press Enter to finish. If your program does not run correctly at first, there is no need to worry. This is a long enough program that there is a good chance you made a mistake in typing somewhere. Almost any small mistake will cause a problem that shows up when you press F5. I can't list all of the possible symptoms that you will see, but here are a couple of examples: 1. A dialog pops up that says There's an error in your program: invalid syntax. Look in the program window and you will see something highlighted in red. The problem is somewhere at or before the place marked red - sometimes even on the line before. Check your typing in that area and try again. 2. It prints a message that begins with Traceback (most recent call last):. Look at the very last line of the message, it will often give you a clue. If you misspelled a name in your program, it will say something like NameError: name 'messag' is not defined. For more help getting your program to run, see Common Problems and Solutions on page 157. Congratulations on getting this far. You have learned some really useful building blocks. Perhaps this little password program has given you some ideas. However, it doesn't seem very useful to run a program with a password-protected message in it when the program window is open right there for anyone to see. Would you like to be able to have your Mom run this program, without her seeing the program code? I thought so. See Appendix 2: How to Let Others Run Your Programs in page 159. Hiding the password while it is being typed To avoid showing the password when it is typed, use the getpass() function from the getpass module (we'll explain modules later). Replace this line in password.py: password = raw_input("what is the password? ") with these two lines: import getpass password = getpass.getpass("what is the password? ") 24

If you run your program from the Python Shell, it prints the following warning: Warning: Problem with getpass. Passwords may be echoed. and prints the password anyway. But if you set up your program to be run from a shortcut using the instructions starting on page 159 the getpass() function will work correctly and not show the password. FYI: Why is there an extra raw_input() call at the end of password.py, with the prompt Press Enter to finish? If you run the program from an icon on the desktop, the program window disappears as soon as it has finished running. The purpose of this line is to keep the program window visible until the user has had time to read what was printed. Quiz 4 1. Change the secret message in password.py. 2. Change the password in password.py. 3. Change password.py so that it doesn't ask for your name and doesn't print it out. After each of these changes, run the program by pressing F5. Fix the program if it doesn't do what you want. 25