CSCI 1012
ex1.py
ex2.py
ex3.py
.zip
with all the .py
files
check_even.py
vs. checkEven.py
def check_even(x):
vs. def check_evens(x):
1 2 3
vs. 1 2 3
Read your autograder output!
s[INDEX]
(where s
is the string and INDEX
is the integer index you are trying to access)L[INDEX]
(where L
is hte list and INDEX
is the integer index you are trying to access)T[INDEX]
(where T
is the tuple and INDEX
is the integer index you are trying to access)D[KEY]
(where KEY
is the key whose value you are trying to access)range()
syntax)Write a function ex1
that takes a list of integers as its only argument. Your function will return a dictionary with two keys: even
whose value is a list of all of the even numbers in the input list (in the order they appeared), and odd
whose value is a list of all of the odd numbers in the input list (in the order they appeared).
ex1([1,2,3,4])
returns {'even': [2, 4], 'odd': [1, 3]}
ex1([4,2,2,1])
returns {'even': [4, 2, 2], 'odd': [1]}
ex1([1,5,7,3])
returns {'even': [], 'odd': [1,5,7,3]}
ex1([])
returns {'even': [], 'odd': []}
Submit as ex1.py
.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
print("hello, world!')
^
SyntaxError: unterminated string literal (detected at line 1)
^
and line number tell us where in the code the error was found
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined
y
and z
before assigning anything to themy
, we should define it first, or change ==
to =
(depending on what we want to do)z
, we should use 'z'
instead
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'builtin_function_or_method' object is not subscriptable
len[y]
: Python trying to index len
, but this can’t be interpreted logically
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'int' object is not subscriptable
x = y[0]
: Python trying to access the 1st element of y
y
is a string, list, tuple, but can’t be interpreted when y
is an int
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'tuple' object is not callable
not callable
means we tried to call something as if it were a function
t
is not a function, it’s a tuple, so it can’t be calledt[0]
instead (i.e., square brackets, instead of parentheses)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'string' object does not support item assignment
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'tuple' object does not support item assignment
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'b'
L = [1,2,4]
for i in range(len(L)):
if L[i] > L[i+1]:
print("The list is not in strictly ascending order")
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: list index out of range
i
is 2
.L[i] (L[2])
against L[i+1] (L[3])`def function_A(x, y):
return function_B(x, y)
def function_B(x, y):
return x + y
print(function_A('3', 1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in function_a
File "<stdin>", line 2, in function_b
TypeError: can only concatenate str (not "int") to str
Code is provided for the following prompt, but it doesn’t run. Your task is to edit it by testing the code, reading the error messages, and fixing the bugs. Submit your edited version as ex2.py.
Write a function ex2 that takes one argument: a dictionary that has lists of integers as its values. Your function should return the maximum value in any of the lists. You may not use the built-in max
function.
ex2({'hello': [2, 34, 5, 1, 0, 2], 'goodbye': [1, 2, 3, 99]})
returns 99
.ex2({'a': [2, 34], 'b': [1, 2], 'c': [-24]})
returns 34
.Code is provided for the following prompt, but it doesn’t yield the intended answer. Your task is to edit it by testing the code, and using print statements to identify the issue(s). Submit your edited code as ex3.py
.
Write a function ex3
that takes one argument: a dictionary that has student names as keys and their grades (0-100) as integers. Your function should return the number of A’s in the class (as an int
; where an A is defined as any grade 90 or above).
ex3({'Julie': 95, 'Anna': 99, 'Oscar': 85})
returns 2
.ex3({'Julie': 95, 'Anna': 99, 'Oscar': 95})
returns 3
.ex3({'Julie': 85, 'Anna': 79})
returns 0
.