Download presentation
Presentation is loading. Please wait.
1
Survey of Computer Science CSCI 110, Spring 2011 Lecture 10 Nested Loops, Exam Review
2
Printing Lines of Asterisks
Suppose you want to print out several lines of asterisks, with the user specifying how many asterisks are in each line: number = input("Please enter the number of asterisks: ") for count in range(number): #1st line of asterisks print "*", #comma suppresses newline print #print a newline for count in range(number): #2nd line of asterisks ...
3
Nested for loops We can use a for loop within a for loop (a nested for loop) to accomplish this more efficiently: height = input("Enter height: ") width = input("Enter width: ") for i in range(height): #draw a line of asterisks for j in range(width): print "*", print #print a newline after each line.
4
Review Problem 1 1) A boolean is a data type that has a value of True or False. Given these boolean variables: x = True, y = False, z = True What is the value (True/False) of each of the following? i) x and (y or z) ii) (not x) or (not(y and z)) iii) y or (x and not z)
5
Review Problem 2 2)Consider a program that does the following:
Prompt the user to input a number between 0 and 100. If the number is greater than 50 and less than 100 write: XX is in the top 50 percent. where XX refers to the number inputted by the user. Otherwise, if the number is less than or equal to 50 write: XX is in the bottom 50 percent. Otherwise, write Illegal number entered. a) Draw a decision tree for the program. b) Write a Python program to execute the program
6
Review Problem 3 3) Compute the value of the following expressions:
c) Add parentheses to part b so that the addition is performed before the division. d) What is the value of c) ?
7
Review Problems 4 and 5 4) What is the value of sum after the execution of the following code? sum = 0 for counter in range(1, 5): sum = sum + 2 * counter 5) What is the output of the following code fragment? mySentence = "It was a dark and stormy night" partial = mySentence[10:13] print partial
8
Review Problem 6 6) What is wrong with the following fragments of Python? a) #Program Error1 number = raw_input("Please enter a number: ") number = number/2.0 print number b) #Program Error2 response = raw_input("Do you like chocolate? ") if response == "yes": print "Me too!" else: print "That's too bad."
9
Review Problem 6 (continued)
6) What is wrong with the following fragments of Python? c) #Program Error3 secret = "csci110" response = raw_input("Enter the password: ") if response = secret: print "Access granted." else: print "Access denied. d) #Program Error4 count = 0 while count < 10 print count count = count + 1
10
Review Problem 7 7) Consider the following while loop: sum = 0
count = 5 while count <= 10: sum = sum + count count = count + 1 a) Create a table for the values of count and sum each time through the loop: b)Write a for loop that does the equivalent of the above loop.
11
Review Problem 8 8) Consider the following Python code: sum = 0
for i in range(1, 5): sum = sum + i print sum What is value of sum printed on the screen?
12
Review Problem 9 9) Fill in the necessary code to fill in the depositList array with deposit amounts entered by the user. You should keep track of the sum of all the deposits made, so that the print statement at the end of the program makes sense. You can assume there are exactly 1000 deposits made. #Program deposit.py depositList = [ ] sum = 0 #your code goes here print "The sum of the deposits: $%6.2f"% sum
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.