ecs10-10-17

ecs10-10-17 - Midterm Midterm Midterm 1 is Monday 20, in...

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: Midterm Midterm Midterm 1 is Monday 20, in class ECS10 10/17 Bring Scantron Bring a Scantron 2000 form form 15--17 15--17 multiple choice questions – 65 pts ? Sh (8 lin Short (8 lines?) program – 35 pts? pr pt Open Open book, open notes. Bring all the notes you want. I expect a doctor’s note or equivalent if you expect have to miss it. Wds Wds Oct 22 is the drop deadline drop Programming Problem Approach First First understand what the program is supposed to do. Here, Here, the program is supposed to 1. 2. 3. 3. Get user input – initial mass, rate of decay Reduce mass by (rate of decay) percent Unitl mass is reduced to < 1% of its initial mass Heart Heart of the program will be a while loop. Start Start in the middle…on scratch paper. While While loop The The program will require a while loop. You really have to understand how it works. while Boolean is True: do block block Choose variables initialMass initialMass - The mass the user inputs at the beginning. Float. mass mass – The mass that decreases every year. Float Float. rate rate – The percent decay every year. Float. year year – The number of years so far. Integer. The The block under the while line is repeated until the Boolean test in the while line becomes False. 1 Block under the while while _________ : mass = mass - mass*rate/100.0 year = year + 1 print ‘After year’,year, print ‘The mass is’, mass What What should the Boolean condition in the while statement be? The The Boolean condition while mass > initialMass*0.01: mass = mass - mass*rate/100.0 year = year + 1 print ‘After year’,year, print ‘The mass is’, mass What What values should all the variables have before the loop starts? Getting user input reply = raw_input(“Enter initial mass (grams):”) initialMass = float(reply) reply = raw_input(“Enter yearly decay rate in percent: percent: “): rate = float(reply) Beginning Beginning values for other variables mass = initialMass year = 0 while mass > initialMass/100: mass = mass - mass * rate / 100 year = year+1 print “After year”,year, print “the mass is”,mass Does Does not say that input has to be checked to avoid crashes; that would be optional. After the loop print “Years needed to drop below 1% of initial\ initial\ mass is”,year Variables Variables and assignment x=0 Variable on left. Expression (something which is or computes a data item) on the right. x = x+1 Uses old value of x on the right to compute new value of x, which is stored in variable x on left. Comments! Comments! Comments! Comments! Comments! Comments! If If you make mistakes in the code, but the comments show what you were trying to do, you might get partial credit. 2 Boolean expressions Either Either have value True or False (capitalized!) x == 0 An An expression that has value either True or False. Use it in an if or a while statement. if whil while x == 0: x = x + random.randrange(2) Complicated Booleans if not (reply == ‘r’ or reply == ‘p’ or reply == ‘s’): True True when reply is NOT ‘r’, ‘p’, or ‘s’ if (reply == ‘R’ and reply == ‘D’): Always Always False, so the block under if never done. if (reply != ‘R’ and reply != ‘D’): True True when reply is not ‘R’ or ‘D’ = or == question The lines: x=0 x == 1 a) Will result in x containing the value 0. b) Will result in x containing the value 1. c) Will result in x containing the value False. d) Will cause an error. = or == question The lines: x=0 x == 1 a) Will result in x containing the value 0. b) Will result in x containing the value 1. c) Will result in x containing the value False. d) Will cause an error. Example if-elif-else question if-elifThe following lines: import random points = 0 x = random.randrange(3) if x == 0: print ‘rock’ elif x <= 1: print ‘paper’ else: print ‘scissors’ Example if-elif-else question if-elifThe following lines: import random points = 0 x = random.randrange(3) if x == 0: print ‘rock’ elif x <= 1: print ‘paper’ else: print ‘scissors’ a) Will print one of ‘rock’,’paper’,’scissors’ b) Might print both ‘rock’ and ‘paper’ c) Will never print ‘rock’ d) Might not print anything. a) Will print one of ‘rock’,’paper’,’scissors’ b) Might print both ‘rock’ and ‘paper’ c) Will never print ‘rock’ d) Might not print anything. 3 random.randrange It’s It’s in the book and 10/15 lecture. Test it out in IDLE. random.rangrange(10) Produces Produces a random number between 0 and 9. Prepare Prepare for if-elif-else question if-elifReview Review lecture notes from 10/15 and rps.py program. Read Read pages 61--64 in book and try out all 61--64 examples in IDLE examples in IDLE. Programming Programming exercise: Write Write a program that gets a random number between 1 and 4, prints it out, and then correctly identifies it with the sentence “It is four”, “It is three”, “It is two”, or “It is one”, using if-elif-elifif-elif-elifelse. Example question Example question Example question Example question 4 Common errors Use Use a variable before you put something in it. Causes the program to crash! Value Value of an expression is not put into a variable. float(reply) balance + (balance*rate) – payment Does Does not cause an error, but the value of all variables remains the same. 5 ...
View Full Document

Ask a homework question - tutors are online