Final Exam Review Spring 2004: Tools for the TA's
Link back to Sp2004 Final Exam Review
Questions and Answers?
def breakout(number):
idnumFile=getMediaPath("students.txt")
file=open(idnumFile, "rt")
idnum=file.read()
file.close()
dayloc=idnum.find(day)
if dayloc != -1:
begline=idnum.rfind("/",0,dayloc)
endline=idnum.find("\n",begline)
print "Breakout Session", idnum[begline:endline]
if dayloc==-1:
print "Not Found"
I posted my code above. I'm having trouble with the begline. Where do I tell it to start looking for the day?
breakout(gtgnum):
breakoutFile = getMediaPath("students.txt")
file = open(breakoutFile, "rt")
breakout = file.read()
file.close()
brk = breakout.find("Breakout" and "Time")
if brk ! = -1:
#something with brkloc and endline??
print "brk"
if brk == -1:
print "Not Found"
I have no idea what I'm supposed to do about the middle part, and no idea how the person who posted the code above figured it out. Hints, anyone? ; ) Karin Bowman
| Neither of these are close. In the first one, what's "day" that you're searching for? In the second one: "Breakout" and "Time"? Mark Guzdial |
Should breakout time and breakout day be found separately? Basically, I should put time and day in two separate pieces? Karin Bowman
def breakout(number):
idnumFile=getMediaPath("students.txt")
file=open(idnumFile, "rt")
idnum=file.read()
file.close()
idloc=idnum.find(number)
if idloc != -1:
startline=("#",0,idloc)
endline=idnum.find("\n",0,idloc)
index=idnum.rfind("/",0, endline)
index2=idnum.rfind("/",0,index)
print "Breakout Session:", idnum[index2+1:endline]
if idloc==-1:
print "Not Found"
I have posted my code above. Something is wrong with it because for every gt number I type in, I get "Tuesday/12:00 - 1:30." I thought maybe adding the startline would help, but it didn't. What's wrong here? Thanks. Lindsey Richardson
| Lindsey, look at where your rfinds are searching through. They only need to go to a certain point on that line you need to find the gtgnum on, yours are going all the way back to 0. Also, once you reverse find to a certain point on a line, you don't want to keep going backwards, you probably want to go foward and try to surround the part you want to print. Summer McWilliams |
Please tell me this is right. If not please give me some pointers! Is it possible to print out the day and time the way I did it or do I have to add the index numbers? Thanks. Melanie Nelson
def breakout(gtgnum):
file=getMediaPath("students.txt")
studentfile=open(file, "rt")
stufile=file.read()
file.close()
gtg=stufile.find(gtgnum)
if gtg!=-1:
first=stufile.find("/",gtg)
second=stufile.find("/",first)
third=stufile.find("/",second)
end=stufile.find(third,"\n")
print stufile [third:end]
if gtg==-1:
print "Not Found"
def breakout(gtnum):
sequencesFile=getMediaPath("students.txt")
file=open(sequencesFile,"rt")
sequences=file.read()
file.close()
gtloc=sequences.find(gtnum)
if gtloc !=1:
search1=sequences.find("#",gtloc)
search2=sequences.rfind("/",search1)
search3=sequences.rfind("/",search2)
print, sequences(search3:search1)
if gtloc== -1:
print "Not found"
I think this is right. Jasmina Pacariz
Jasmina- your code doesn't work. First off, you need square brackets in your print statement, not parenthesis.
ok thanx.Must have missed that one. But it should work after that right? Jasmina Pacariz
for me it doesn't seem to return anything. Have you tried running it in JES?
| Jasmina, talk through your code, because it doesn't print anything if you try to run it in JES. You start from the # at the beginning of the line after the one on which your gtgnum is located, which is a good idea. Then, you do two rfinds, but you don't have all the variables you need in them (rfind needs what its looking for, a point to stop at, and a point to start working backwards from). Try again and see what you can come up with. Summer McWilliams |
def breakout(gtnum):
file.open("students.txt", "rt")
sfile = file.read()
file.close()
gtg = sfile.find(gtgnum)
first = sfile.find("/", gtg)
second = sfile.find("/", first)
third = sfile.find("/", second)
endofline = sfile.find("/n", third)
print sfile[third:endofline]
~how do you add the not found part.....i forgot about that
| To check for not found, check if .find is returning -1. Mark Guzdial |
def breakout(gtgnum):
gtgList = getMediaPath("students.txt")
list = open(gtgList, "rt")
student = list.read()
list.close()
stuloc = student.find(gtgnum)
if stuloc != -1:
gtgloc = student.rfind("/", 0, stuloc)
begline = student.rfind("/", 0, gtgloc)
endline = student.find("\n", gtgloc)
print student[begline:endline]
if stuloc == -1:
print "Not found"
for each gtg i type in, i get the day and time for the previous line.. i've tried just about everything, i'm not sure what i'm doing wrong.. Sana Yousufi
| Sana, it's because you're doing rfind's instead of find's. You're finding the gtgnum, then searching from there BACKWARDS to "/" – that's on the previous line. Mark Guzdial |
def breakout(gtgnum):
gtgList = getMediaPath("students.txt")
list = open(gtgList, "rt")
student = list.read()
list.close()
stuloc = student.find(gtgnum)
if stuloc != -1:
gtgloc = student.find("/", 0, stuloc)
begline = student.find("/", 0, gtgloc)
endline = student.find("\n", gtgloc)
print student[begline:endline]
if stuloc == -1:
print "Not found"
???
Here's my code guys, it seems to work just fine... but don't forget to create the students.txt file on JES folder before you run it.
def breakout(gtnum):
stdfile = getMediaPath("students.txt")
file = open(stdfile, "rt")
stds = file.read()
file.close()
gtloc = stds.find(gtnum)
if gtloc == -1:
print "Not found"
if gtloc != -1:
slash1 = stds.find("/", gtloc)
slash2 = stds.find("/", slash1+1)
slash3 = stds.find("/", slash2+1)
endpoint = stds.find("\n", slash3+1)
print stds[slash3+1:endpoint]
Hey for the above person.. Did you mean to put "/n" instead of "\n"?
def breakout(gtgnum):
gtgList = getMediaPath("students.doc")
file = open(gtgList, "rt")
student = file.read()
file.close()
stuloc = student.find(gtgnum)
if stuloc != -1:
first = file.find("/", stuloc)
second = file.find("/", first)
third = file.find("/", second)
endofline = file.find("/n", third)
print file[third:endofline]
if stuloc == -1:
print "Not Found"
The error I get is this:
You are trying to access a part of the object that doesn't exist.
in file C:\JES\Final1.py, on line 8, in function breakout
AttributeError: instance of 'org.python.core.PyFile' has no attribute 'find'
Please check line 8 of C:\JES\Final1.py
What's wrong? How is there not a find?
| There is a find, that works for strings, not for files. I think you meant student.find where you said file.find. Mark Guzdial |
Does JES stop looking after it finds the 1st slash when it executes: slash1 = stds.find("/", gtloc)
def breakout(gtnum):
file.open("students.txt", "rt")
sfile = file.read()
file.close()
gtg = sfile.find(gtgnum)
if gtg != -1:
first = sfile.find("/", gtg)
second = sfile.find("/", first)
third = sfile.find("/", second)
endofline = sfile.find("/n", third)
print sfile[third:endofline]
if gtg ==-1:
print "Not Found"
Is this correct? sorry, I can't test it, I'm at my parents, and their computer is slow and without JES. THANKS!
In the above programs, when something like this appears:
first = file.find("/", stuloc)
what comes first in the actual file: the / or the thing rep'd by "stuloc"? It appears that you flip flop them from their actual order when you refer to them in parentheses? Is that correct?
In this line of Sana's code, what doth the zero represent?
gtgloc = student.rfind("/", 0, stuloc)
Jonathan Laing
def breakout(gtgnum):
gtgList = getMediaPath("students.txt")
list = open(gtgList, "rt")
student = list.read()
list.close()
stuloc = student.find(gtgnum)
if stuloc != -1:
gtgloc = student.find("/", stuloc)
begline = student.find("/", gtgloc)
midline = student.find("/", begline +1)
midline2 = student.find("/", midline +1)
endline = student.find("\n", gtgloc)
print student[midline2 + 1:endline]
if stuloc == -1:
print "Not found"
I changed my code around, and now it seems to work fine. Thanks Prof Guzdial. Sana Yousufi
Is end of line "/n" or "\n"? In the above solutions it is being done both ways. Which is correct?
| End of line is "\n". Jonathan, you need to look up find and rfind in the book. Mark Guzdial |
Thanks, that helped. Here's another question for you:
I'm trying to run my code but I get the following error:
IOError: File not found - C:\JES\student.txt (The system cannot find the file specified)
Please check line 3 of C:\JES\finalTAtool.py
I know I saved the file in JES as student.txt as a text file. Any ideas?
Here's what I've got so far:
def breakout(gtgnum):
studentsfile = getMediaPath("student.txt")
file = open(studentsfile, "rt")
students = file.read
file.close
curloc = students.find(gtgnum)
if curloc == -1:
print "Not Found"
if curloc != -1:
dayloc = students.find("/", curloc+2)
endline = students.find("\n", dayloc)
print student[dayloc:endline]
Jonathan Laing
Did you remember to set the media path?
| setMediaPath should not matter at all if he's not using getMediaPath. Are you sure you saved it into C:\JES? Mark Guzdial |
Yes, I saved in C:\JES. I even resaved it. I also tried quitting JES and reopening the code. I'm also using the same document name in the code as I saved it as. I have no idea why it won't open the file.
It's not giving any errors when I load it. I'm executing breakout("gtg123a") in the command area.
Jonathan Laing
When I was doing the "handling addressbook" problem. I saved the text file as "contacts.txt" so that the filename was like contacts.txt.txt or some such crazy thing... so I made a new file and made sure to name it just "contacts" and then it worked - no problem. That may not help at all, but thought I'd mention it.
- also you are using getMediaPath, right? It's part of studentsfile...? Or does it say getMediaPath, but is really doing something else??
def breakout(gtgnum):
breakoutFile = getMediaPath("students.txt")
file = open(breakoutFile,"rt")
breakout = file.read()
file.close()
findgtg=breakout.find(gtgnum)
if findgtg != -1:
first=breakout.find("/", findgtg)
second=breakout.find("/", first)
third=breakout.find("/", second)
end=breakout.find("#", third)
print breakout[third:end]
if findgtg==-1:
print "Not Found"
This returns
"/George P. Burdell/Summer McWilliams, Dannon Baker/Tuesday/12:00-1:30"
Why is it returning ALL of it???? It should be returning only from the third slash until the beginning of the next row(#)
Amelia Cipolla
| Jonathan, try spelling out the whole path: open(r"C:\JES\students.txt","rt") and see if that helps. Amelia, I'm not seeing where the problem is, either. Try these two things: Show us what students.txt looks like, and insert just before your existing print statement: print findgtg, first, second, third, end just so that we can see what it's matching to. Mark Guzdial |
#gtg123a/George P. Burdell/Summer McWilliams, Dannon Baker/Tuesday/12:00-1:30
#gte444q/Howard T. Duck/Adam Wilson, Ryan McGarty/Wednesday/1:30-3:00
#gtg563b/Mickey M. Mouse/Kelly Farrell, Stephanie Weitzel/Tuesday/3:00-4:30
that is saved as students.txt in my mediasources folder
def breakout(gtgnum):
breakoutFile = getMediaPath("students.txt")
file = open(breakoutFile,"rt")
breakout = file.read()
file.close()
findgtg=breakout.find(gtgnum)
if findgtg != -1:
first=breakout.find("/", findgtg)
second=breakout.find("/", first)
third=breakout.find("/", second)
end=breakout.find("#", third)
print findgtg, first, second, third, end
print breakout[third:end]
if findgtg==-1:
print "Not Found"
Put in:
breakout("gtg123a")
prints out:
1 8 8 8 78
/George P. Burdell/Summer McWilliams, Dannon Baker/Tuesday/12:00-1:30
Amelia Cipolla
| Amelia, try second = breakout.find("/",first+1) and similarly add 1 all the way down. See how first, second, and third are all reporting the same number? They're all finding the same slash! Mark Guzdial |
No luck on opening that file, even after doing what you said, Prof. Guzial. Jonathan Laing
| I just reread what you had, Jonathan. Why do you have getMediaPath in there?!? That's generating an incorrect path for you. Just open the filename. Mark Guzdial |
def breakout (gtnum)
studentFile=getMediaPath("students.txt")
file=open(studentFile, "rt")
students=file.read()
file.close()
slashloc=students.find(gtnum)
if slashloc!=-1:
loc1=students.find("/", slashloc)
loc2=students.find("/", loc1+1)
loc3=students.find("/", loc2+1)
endline=students.find("\n", loc3)
print students [loc3+1:endline]
if slashloc==-1:
print "Student not found."
That should be right :-D
Link to this Page