ecs10-10-19

ecs10-10-19 - Midterm Midterm 1 is Monday 22, in class...

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 1 is Monday 22, in class ECS10 10/17 Bring a Scantron 2000 form 15--17 15--17 multiple choice questions 65 pts ? Short lines?) pr r m Sh rt (8 lin s?) program 35 pts? I expect a doctor's note or equivalent if you have to miss it. Wds Oct 24 is the drop deadline Programming Problem Approach First understand what the program is supposed to do. Here, the program is supposed to 1. 2. 3. Get G user i input initial mass, rate of decay i ii l fd Reduce mass by (rate of decay) percent Unitl mass is reduced to < 1% of its initial mass Heart of the program will be a while loop. First, need to use raw_input() and convert raw_input() strings to ints or floats. Getting user input strIn = raw_input("Enter initial mass (grams):") raw_input("Enter initialMass = float(strIn) float(strIn) strIn = raw_input("Enter yearly decay rate in \ raw_input("Enter percent (0-100): "): (0): decay = float(strIn) float(strIn) The basic while loop while mass > initialMass/100: initialMass/100: mass = mass * decay / 100 Write the basic loop first (maybe on scrap paper). What variables will you need? What is the Boolean expression in the while statement that will control how many times it runs? Most important part of the program and will have a big influence on partial credit. Does not say that input has to be checked to avoid crashes; that would be optional. 1 Fill out the basic loop mass = initialMass year = 0 while mass > initialMass/100: initialMass/100: mass = mass * decay / 100 year = year+1 print "After year",year, year",year, print "the mass is",mass After the loop print "Years needed to drop below 1% of initial\ initial\ mass is",year Comments! Comments! Comments! If you make mistakes in the code, but the comments show what you were trying to do, you might get partial credit. Example string question The following lines will print: strData = "cow" i=1 while i < len(strData): len(strData): print strData[i], strData[ i = i+1 a) cow b) co c) ow d) It will cause an error because strData is not a function Example string question The following lines will print: strData = "cow" i=1 while i < len(strData): len(strData): print strData[i], strData[ i = i+1 a) cow b) co c) ow d) It will cause an error because strData is not a function To prepare for string question Go over Lecture from 10/15, study prettyPrint.py program Read pages 95-99 in book and try out all 95examples in IDLE Programming exercise: Write a program that gets an input string from the user, and doubles every character. For instance, if the user enters "cow", your program prints "ccooww". ccooww". Example if-elif-else question if-elifAfter the following lines: import random points = 0 x = random.randint(1,3)a) points might be 1,2, or 3 random.randint(1,3) if x == 3: b)points might be 6,3, or 0 points = points+3 c) points might be 3,1, or 0 elif x == 2: d)points will always be 0 points = points+1 e) None of the above else: points = 0 2 Example if-elif-else question if-elifAfter the following lines: import random points = 0 x = random.randint(1,3)a) points might be 1,2, or 3 random.randint(1,3) if x == 3: b)points might be 6,3, or 0 points = points+3 c) points might be 3,1, or 0 elif x == 2: d)points will always be 0 points = points+1 e) None of the above else: points = 0 Prepare for if-elif-else question if-elifReview lecture notes from 10/17 and rps.py program. Read pages 61--64 in book and try out all 61--64 examples in IDLE IDLE. Programming exercise: 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 3 ...
View Full Document

Ask a homework question - tutors are online