



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Midterm 1 is Monday 20, in classMidterm 1 is Monday 20, in class Bring aBring a ScantronScantron 2000 2000 formform 15 15----17 multiple choice questions17 multiple choice questions –– 65 pts ?65 pts? Sh rt (8 linSh rt (8 linShort (8 lines?) programShort (8 lines?) program – ?) pr?) pr r mr m – 35 pts35 pt35 pt ?35 pts??? Open book, open notes. Bring all the notes youOpen book, open notes. Bring all the notes you want.want. I expect a doctor’s note or equivalent if youI expect a doctor’s note or equivalent if you have to miss it.have to miss it. WdsWds Oct 22 is theOct 22 is the drop deadlinedrop deadline
First understand what the program is supposedFirst understand what the program is supposed to do.to do. Here, the program is supposed toHere, the program is supposed to 1.1. GGGet user inputGet user input – ii – initial mass, rate of decayi i i li i i linitial mass, rate of decay f df d 2.2. Reduce mass by (rate of decay) percentReduce mass by (rate of decay) percent 3.3. UnitlUnitl mass is reduced to < 1% of its initial massmass is reduced to < 1% of its initial mass Heart of the program will be a while loop.Heart of the program will be a while loop. Start in the middle…on scratch paper.Start in the middle…on scratch paper.
The program will require a while loop. YouThe program will require a while loop. You really have to understand how it works.really have to understand how it works. while Boolean is True: do blockdo block
The block under the while line is repeated untilThe block under the while line is repeated until the Boolean test in the while line becomes False.the Boolean test in the while line becomes False.
initialMassinitialMass -- The mass the user inputs at theThe mass the user inputs at the beginning. Float.beginning. Float. massmass –– The mass that decreases every year.The mass that decreases every year. FloatFloatFloat.Float. raterate –– The percent decay every year. Float.The percent decay every year. Float. yearyear –– The number of years so far. Integer.The number of years so far. Integer.
while _________ : mass = mass - mass*rate/100. year = year + 1 print ‘After year’,year, print ‘The mass is’, mass
What should the Boolean condition in the whileWhat should the Boolean condition in the while statement be?statement be?
while mass > initialMass0.01: mass = mass - massrate/100. year = year + 1 print ‘After year’,year, print ‘The mass is’, mass
What values should all the variables have beforeWhat values should all the variables have before the loop starts?the loop starts?
reply = raw_input(“Enter initial mass (grams):”) initialMass = float(reply) reply = raw_input(“Enter yearly decay rate in percent: “):percent: ): rate = float(reply)
Does not say that input has to be checked toDoes not say that input has to be checked to avoid crashes; that would be optional.avoid crashes; that would be optional.
mass = initialMass year = 0 while mass > initialMass/100: mass = mass - mass * rate / 100 year = year+ print “After year”,year, print “the mass is”,mass
print “Years needed to drop below 1% of initialprint “Years needed to drop below 1% of initial\ massmass is”,yearis”,year
Comments! Comments! Comments!Comments! Comments! Comments!Comments!Comments! Comments! Comments! Comments! Comments! If you make mistakes in the code, but theIf you make mistakes in the code, but the comments show what you were trying to do, youcomments show what you were trying to do, you might get partial credit.might get partial credit.
x = 0
Variable on left. Expression (something which is or computes a data item) on the right.
x = x+ Uses old value of x on the right to compute new value of x, which is stored in variable x on left.
It’s in the book and 10/15 lecture. Test it out inIt’s in the book and 10/15 lecture. Test it out in IDLE.IDLE. random.rangrange(10)
Produces a random number between 0 and 9.Produces a random number between 0 and 9.
Review lecture notes from 10/15 and rps.pyReview lecture notes from 10/15 and rps.py program.program. Read pages 61Read pages 61----64 in book and try out all64 in book and try out all examples in IDLEexamples in IDLEexamples in IDLE.examples in IDLE. Programming exercise:Programming exercise: Write a program that gets a random numberWrite a program that gets a random number between 1 and 4, prints it out, and then correctlybetween 1 and 4, prints it out, and then correctly identifies it with the sentence “It is four”, “It isidentifies it with the sentence “It is four”, “It is three”, “It is two”, or “It is one”, using ifthree”, “It is two”, or “It is one”, using if--elifelif--elifelif-- else.else.
Use a variable before you put something in it.Use a variable before you put something in it. Causes the program to crash!Causes the program to crash! Value of an expression is not put into a variable.Value of an expression is not put into a variable.
float(reply) balance + (balance*rate) – payment
Does not cause an error, but the value of allDoes not cause an error, but the value of all variables remains the same.variables remains the same.