ecs10-10-8

ecs10-10-8 - Last time ECS10 10/8 Boolean Boolean data –...

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: Last time ECS10 10/8 Boolean Boolean data – True or False Boolean Boolean operators: and, or, not Use Use to check for valid user input: reply = raw_input(‘Enter R or D: ’) if not ((reply == ‘R’ ) or (reply == ‘D’ )): print ‘Not one of the choices.’ Ask until we get one of the inputs we understand May May have to ask an arbitrary number of times. Use while Use a while loop. while Boolean is True: Boolean Do statements in block A loop while not ((choice == 'D') or (choice == 'R')): print 'Not a major party.' choice = raw_input('R or D: ' ) print 'Thank you.‘ Like if, the block under while is done when its Boolean test is True. Unlike if, after doing the block, the program goes back up to the while statement. while while loop itereates over block – does block over and over, until Boolean becomes False somehow. Infinite Infinite loop One One of the classic programming bugs Get Get out of it using CRTL-c (hold down CRTLcontrol key and type c) Repeat after me: CRTL-c CRTL Unintended Unintended infinite loop while ((choice != 'D') or (choice != 'R')): print 'Not a major party.' choice = raw_input('R or D: ' ) Infinite loop because the Boolean condition is always True! 1 Converting types int('5')/2 Converts the string ‘5’ to the integer 5 to Converting Data Convert Convert an integer to a string: x = str(10) x is the string ‘10’ is y = float(‘8.3333’) y is the floating point number 8.3333 Getting numbers from the user reply = raw_input(‘Feet: ‘) feet = int(reply) reply = raw_input(‘Inches: ‘) inches = int(reply) Notice we use the variable reply twice. When we put the second string into reply, the first one gets written over. It’s gone! Body mass index Kilograms / (meters2) Or, Or, in `English Imperial Units’ (Pounds * 703) / (inches2) 25 25 or greater is overweight. Yao Ming Next assignment: compound compound interest program Original Original debt is $10,000 Every month, make a payment and pay interest Loop Loop until debt is paid off 7 feet, 6 inches. 310 pounds. 2 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online