The part of the final dealing with material covered before the midterm will be small, 2-4 questions at most. The final will emphasize material from May 9 (Programming languages and the Windows System) on. Some of the questions will be on the Python parts of the course. These questions will explore various aspects of Python and of programming. The central themes are writing a program, data types (with variables), operators, selection, and looping. These questions are sample Python questions. The other questions will be like the ones asked on the midterm.
x = 3 if 2 > x : print 'First' else : print 'Second' if 2 > x : print 'Third' print 'Fourth' print `Fifth'Answer:
Second Fourth Fifth
words = 'this IS NoT EvEN' print words.title() print words.replace("IS", 'was') print words.upper() print words * 2Answer:
This Is Not Even this was NoT EvEN THIS IS NOT EVEN this IS NoT EvENthis IS NoT EvEN
line = raw_input("Type a word") print "You typed", line line = line + "h" num = int(line) print "You typed the number ", numAnswer: The problem lies in lines 3 and 4. Line 3 appends a letter ("h") to the input string. Line 4 then tries to convert the result to an integer. The result, however, will never be a valid integer because of the letter "h". So you get an error.
Answer: Here is my version, in pseudocode.
prompt the user for an integer input read the input and convert it to an integer while the integer is not 0 if the integer is 100 or more, print "it is hot" if the integer is 60 or less, print "it is cold" otherwise, print "it is just right" prompt the user for an integer input read the input and convert it to an integer
Please enter a temperature: 95 It is just right. Please enter a temperature: 110 It is hot. Please enter a temperature: 32 It is cold. Please enter a temperature: 0 Good bye!Answer: Building on the pseudocode in the answer to question 7, we get:
# # pseudocode: # prompt the user for an integer input # read the input and convert it to an integer # temp = int(raw_input("Enter temperature please: ")) # # pseudocode: while the integer is not 0 # while temp != 0: # # pseudocode: # if the integer is 100 or more, print "it is hot" # if temp >= 100: print "it is hot" # # pseudocode: # if the integer is 60 or less, print "it is cold" # elif temp <= 60: print "it is cold" # # pseudocode: # otherwise, print "it is just right" # else: print "it is just right" # # pseudocode: # prompt the user for an integer input # read the input and convert it to an integer # temp = int(raw_input("Enter temperature please: ")) # # Say goodbye # print "Good bye!"