exam 1 review cs 1803
DESCRIPTION
From section IE3TRANSCRIPT
CS 1803 Recitation 9/20
Announcements OH NOES! AN EXAM IS WEDNESDAY!
What should you study?
No homework is due this week because of the exam.
It’ll be released tomorrow.
HW3 should be almost completely graded.
If you don’t have your grade, you should very soon.
Review session is tomorrow at 6 PM in Boggs B6
Yours TAs truly will be there!
Read the grading rubric at the bottom of every assignment.
We grade your stuff according to that rubric.
HW2 comments Duplication of code It’s a problem.
Don’t do it.
Srsly.
Things that don’t work: if int(inputString) == int: Why?
myList.append([stuff]) Why?
The use of global
Checking for bad input!!!!!! OMGOSH! CHECK IT!
This was the biggest problem on HW3.
Exam ReviewWe have not seen the exam…
Your guess is as good as ours.
We won’t see the exam until you do.
Q/A time
Written Practice Questions
Practice Question 1 Trace the following code and write down the output:
def myFunc(x):
if(len(x) <=0):
print(“Done!”)
else:
print(x)
myFunc(x[:len(x)-1])
myFunc(“This is a string.”)
Practice Question 1 Solution This is a string.
This is a string
This is a strin
This is a stri
This is a str
This is a st
This is a s
This is a
This is a
This is
This is
This i
This
This
Thi
Th
T
Done!
Practice Question 2Write a function reverseList() that will take in a list as a
parameter and return the original list in reverse order.
Practice Question 2 Solution def reverseList(x):
rList = []
for i in range(-1, (len(x)*-1)-1, -1):
print("i:", i, "list:", x[i])
rList.append(x[i])
return rList
Practice Question 2 Solution def reverseList(x):
l=len(x) -1
new=[]
while(i>0)
new.append(x[i])
i = i-1
return new
Practice Question 3 Briefly explain the use of the global keyword in Python. Why
it is needed?
Practice Question 3 Solution The global keyword is used when we wish for a function to
modify a variable that is declared above the function’s scope.
All calls to the function will use the same instance of the
global variable. It is needed when we wish to use the same
variable among multiple functions.
Practice Question 4Write a function named averageList(), which takes in no
parameters, which will prompt the user to enter a set of
numbers, separated by commas (You may assume the user
will follow your directions; you do not need to include error
checking). Then, you must print the average of the list to 3
decimal places.
Practice Question 4 Solution def averageList():
st = input("Enter the list of numbers: ")
stList = st.split(',')
total = 0
for eachNumber in stList:
total += float(eachNumber)
total /= len(stList)
print("The average is: %.3f"%total)