{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern