ecs10-10-3

ecs10-10-3 - 10/3/2008 Last Time Variables Variables ECS 10...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10/3/2008 Last Time Variables Variables ECS 10 10/3 name = ‘Rex’ Variable on left, =, then data Assigns data to variable Getting user input name = raw_input(‘Enter name: ‘) Assigns string that user types to variable name Concatenation ‘Rex’+’,’ The empty string The The variable name contains a string, but it is a string containing no characters. characters. It It is called the empty string. string. Th The length of string ‘Daniel’ is 6 ‘D The The length of the empty string ‘’ is 0 Checking for empty string Using Using the if statement: if if name == ‘’ : print ‘Welcome, anonymous!’ If If the variable name contains the empty string, then print ‘Welcome, anonymous!’. Otherwise, Otherwise, don’t. The if statement if name == ‘’ : print ‘Welcome, anonymous’ if condition: condition Python commands = vs == = is used in assignment statements, to assign a is data value to a variable name = ‘Rex’ == is used in if statements == if name == ‘’: print ‘Hello, anonymous!’ 1 10/3/2008 Malformed if statements if if name = ‘’: print ‘Welcome!’ Malformed if statements if if name = ‘’: print ‘Welcome!’ Uses = instead of == Missing : No indentation if if name == ‘’ print ‘Welcome!’ if if name == ‘’ print ‘Welcome!’ if if name == ‘’: print ‘Welcome!’ if if name == ‘’: print ‘Welcome!’ Block You You can have lots of statements under an if statement. if name = ‘’: name = ‘Anonymous’ print ‘Welcome, Anonymous!’ print ‘Let’s play a game,’ name+’.’ Block The The indented statements form a block. A block. block is always done together, or skipped together. if if name == ‘’: name = ‘Anonymous’ print ‘Welcome, whoever you are!’ print ‘Let’s play a game,’ name + ‘.’ if … else… if name == ‘Fido’ : species = ‘dog’ if … elif…else elif…else if name == ‘Fido’: species = ‘dog’ else: species = ‘human’ Either the block under if is done, or the block under else. or The command else is NOT indented. In IDLE, use backspace key to get back to left side. elif name == ‘Fluffy’: species = ‘cat’ else: species = ‘human’ 2 10/3/2008 if…elif…else if…elif…else Only one Only one of the blocks gets executed. Trys Trys first condition; if that works, execute that block. If not, try second condition. If If If that fails too, the else gets executed. More than one way to skin… species = ‘human’ if name == ‘Fido’: species = ‘dog’ elif name == ‘Fluffy’: species = ‘cat’ Battleground states program Web Web site, demo of my program. 3 ...
View Full Document

This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.

Ask a homework question - tutors are online