Sample Midterm 2
- Evaluate each expression. If any cannot be evaluated, say why.
- 3 + 5.0
- "hello" + "goodbye"
- 7 * True
- 6 / "spam"
- True and not False or True
- "monty" <= "python"
- Convert the following into Python:
- “hello” follows “goodbye” in the
character ordering of the computer
- The value of the variable answer is true or the value
of the variable y is less than 126
- The function gleep is called with an argument of
−21
- Convert the following into a “for” loop:
i = 1
while i < 10:
print i * "x"
i += 3
- What does the following function do when given a string as the
first argument and a string containing only one character as the
second argument?
def f(s, c):
i = 0
while i < len(s):
if s[i] == c:
return i
i += 1
return -1
- In the following program:
try:
x = input("Enter a number: ");
y = 1 / x;
except ZeroDivisionError:
print "Nice try: you divided by 0"
except SyntaxError:
print "What on earth did you type?"
except NameError:
print "I have no clue what that name refers to"
except TypeError:
print "I don't handle conversions well today"
else:
print "The reciprocal of", x, "is", y;
please show five different inputs, each of which will execute a different
print statement in the program.