# p1.py - Practice question 1. # Given a list of 10 numbers, see if it contains a 3, and tell me # the location of the 3. L = [ 8, 0, 4, 5, 4, 7, 3, 2, 6, 1 ] # Before we start looking, let's assume the number # is not in the list. Then we should start with # a bogus location, like a negative address. location = -1 found = False for i in range(0, 10): if L[i] == 3: found = True location = i if found == True: print "I found the number 3 at location " + str(location) else: print "I could not find the number 3 in the list" # ------------------------------------------------------------------ # p2.py - Practice question 2. # Find the largest of 5 numbers in a list. L = [ 5, 4, 7, 3, 2 ] # Let's begin by assuming that the first number is the largest. # For each number in the list, if it's larger, then reset max # to that value. max = L[0] for number in L: if number > max: max = number print "The largest number is " + str(max) # ------------------------------------------------------------------ # p3.py - Practice question 3 # Find the average of 5 numbers in a list L = [ 5, 4, 7, 3, 2] sum = 0 for number in L: sum = sum + number average = sum / 5 print "The average is " + str(average) # ------------------------------------------------------------------ # p4.py - Practice question 4 # Given a word, see if it has a double letter. # For example, here is a 10-letter word. word = "accounting" foundDouble = False for i in range(0, 9): # don't go too far! if word[i] == word[i+1]: foundDouble = True if foundDouble == True: print "Yes, there is a double letter" else: print "No, there is no double letter" # ------------------------------------------------------------------ # p5.py - Practice question 5 # Given 7 cards, determine how many are face cards. hand = [ 'A', '7', '3', 'Q', 'K', '8', '9' ] facecards = 0 for card in hand: if card == 'J' or card == 'Q' or card == 'K': facecards = facecards + 1 print "I found " + str(facecards) + " facecards" # ------------------------------------------------------------------ # p6.py - Practice question 6 # Print the first 20 powers of 2. # Note: to raise a number to a power, use '**' for i in range (1, 21): print 2**i # ------------------------------------------------------------------ # p7.py - Practice question 7 # Ask the user for 3 sides of a triangle. # Determine if the triangle is equilateral, isosceles or scalene. # Let's assume that the sides are in increasing (ascending) length. print ("Please enter the 3 sides of the triangle in increasing order.") a = input("Enter first side of triangle") b = input("Enter second side") c = input("Enter third side") if a == b and b == c: print "Your triangle is equilateral." elif a == b or a == c or b == c: # note word "elif" print "Your triangle is isosceles." else: print "Your triangle is scalene." # ------------------------------------------------------------------ # p8.py - Practice question 8 # Ask the user for 3 sides of a triangle, and see if it's a right triangle. # Let's assume the numbers are entered in ascending order. print ("Please enter sides of triangle in increasing order.") a = input("Please enter side a") b = input("Please enter side b") c = input("Please enter side c") if c*c == a*a + b*b: print "You have a right triangle!" else: print "Your triangle is not a right triangle." # ------------------------------------------------------------------ # p9.py - Practice question 9 # Ask user for number, and print all its divisors. value = input("Please enter a positive number.") print "Here are the divisors of your number:" for i in range (1, value+1): if value % i == 0: print i