|
View:
New views
19 Messages
—
Rating Filter:
Alert me
|
|
|
bug in exam score conversion programHello!!
I just completed exercise 7 (chapter 4) in Zelle's book: "A certain CS professor gives 100-point exams that are graded on the scale 90–100:A, 80–89:B, 70–79:C, 60–69:D, 60:F. Write a program that accepts an exam score as input and prints out the corresponding grade." I am quite happy with my code, but there is a bug: if the score is 100, then the program calculates 100/10 = 10. However, the tuple runs to 9, leaving me with an error message: IndexError: tuple index out of range I can't figure out how to solve that problem... I also suspect that my code clearly exposes me as a beginner :-) What would be the pythonic way of solving that exercise? # exam score to grade conversion # Zelle, ch. 4, exercise 7 x = ("F", "F", "F", "F", "F", "E", "D", "C", "B", "A") score = raw_input("What's your exam score (0-100)? ") grade = x[int(score)/10] print "Your grade is:", grade I greatly appreciate your comments! David _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion programOn Sat, Oct 4, 2008 at 12:11, David <ldl08@...> wrote:
> I am quite happy with my code, but there is a bug: if the score is 100, then > the program calculates 100/10 = 10. However, the tuple runs to 9, leaving me > with an error message: IndexError: tuple index out of range > > I can't figure out how to solve that problem... > I also suspect that my code clearly exposes me as a beginner :-) What would > be the pythonic way of solving that exercise? > > # exam score to grade conversion > # Zelle, ch. 4, exercise 7 > x = ("F", "F", "F", "F", "F", "E", "D", "C", "B", "A") > score = raw_input("What's your exam score (0-100)? ") > grade = x[int(score)/10] > print "Your grade is:", grade Python starts counting from zero so to access the first item in x it would be x[0]. When you do x[10] you actually are requestting the 11th value in x instead of the 10th and this does not exist. You should be able to fix this yourself now. Greets Sander _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion programOn Sat, Oct 4, 2008 at 5:11 AM, David <ldl08@...> wrote:
Hello!! <snip> I can't figure out how to solve that problem... Wow! That's actually a really surprising way of solving this, and not entirely bad. The easiest way of modifying this program is to do exactly what you did with the "F"s, only with an "A".
I suspect most people would have created an if else chain: if score >= 90: grade = "A" elif score >= 80: grade = "B"
elif score >= 70: grade = "C" elif score >= 60: grade = "D" else: grade = "F" print "Your grade is %s" % (grade, )
But in reality, there's nothing wrong with your method - as a matter of fact, it's probably slightly more optimized (although we're talking in time frames of < .00001 seconds) because rather than making comparisons it makes one computation and then directly accesses the values stored in the tuple.
So, I'll compliment you for solving a problem in a way that I never would have thought of, helping me to think outside the box! In addition a tuple is an ideal variable here because it's constant - you cannot change the values or size of a tuple, unlike a list or dictionary. Not that (at this point) you're worried about malicious type users, but it's always a good idea to "fool proof" your code.
Well, I hope this helps both look at your problem a different way, as well as solves your "bug" (in this case, a syntax error: the code behaves exactly how it should, you just made an error in how you implemented your code. Those will be your most common "bugs", from here until forever ;) )
-Wayne _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion program"David" <ldl08@...> wrote
> I am quite happy with my code, but there is a bug: if the score is > 100, then the program calculates 100/10 = 10. However, the tuple > runs to 9, leaving me with an error message: IndexError: tuple index > out of range > > I can't figure out how to solve that problem... > I also suspect that my code clearly exposes me as a beginner :-) > What would be the pythonic way of solving that exercise? > > # exam score to grade conversion > # Zelle, ch. 4, exercise 7 > x = ("F", "F", "F", "F", "F", "E", "D", "C", "B", "A") > score = raw_input("What's your exam score (0-100)? ") > grade = x[int(score)/10] > print "Your grade is:", grade It's not too bad but I would probably use a dictionary rather than the list - which avoids the index problem - and I'd do the int conversion with raw_input:: Grades = {0:'F', 1:'F',2:'F',....8:'B', 9:'A',10:'A'} score = int(raw_input("What's your exam score (0-100)? ")) print "Your grade is:", Grades[score/10] HTH, Alan G _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion program-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 David wrote: > Hello!! > > I just completed exercise 7 (chapter 4) in Zelle's book: > "A certain CS professor gives 100-point exams that are graded on the > scale 90–100:A, 80–89:B, 70–79:C, 60–69:D, 60:F. Write a program that > accepts an exam score as input and prints out the corresponding grade." > Just to throw in another method, I tend to use tables of for problems like this. The requirements usually change so its easier to modify later: # min, max, grade grades = [ (90,100,'A'), (80, 89,'B'), (70, 79,'C'), (60, 69,'D'), ( 0, 59,'F'), ] def getGrade(score): """ Return a letter grade based on a score """ for g in grades: if (score <= g[1]) and (score >= g[0]): return g[2] - -- - ---[Office 71.6F]--[Outside 55.4F]--[Server 107.9F]--[Coaster 71.7F]--- - ---[ WSF KITSAP (366772980) @ 47 34.7811 -122 27.7554 ]--- Software, Linux, Microcontrollers http://www.brianlane.com AIS Parser SDK http://www.aisparser.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Remember Lexington Green! iD8DBQFI535RIftj/pcSws0RAldqAJ9yKYSyDArc/LZ6G47SwxUq4z8yAACgioyx b9WnwDEQe8hSOuYbKuKo9sY= =7lCV -----END PGP SIGNATURE----- _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion program"Brian C. Lane" <bcl@...> wrote > # min, max, grade > grades = [ (90,100,'A'), > (80, 89,'B'), > (70, 79,'C'), > (60, 69,'D'), > ( 0, 59,'F'), > ] > > def getGrade(score): > """ > Return a letter grade based on a score > """ > for g in grades: > if (score <= g[1]) and (score >= g[0]): > return g[2] Could be written more concisely as for g in grades: if g[0] <= score <= g[1]: return g[2] -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld > > > > - -- > - ---[Office 71.6F]--[Outside 55.4F]--[Server 107.9F]--[Coaster > 71.7F]--- > - ---[ WSF KITSAP (366772980) @ 47 34.7811 -122 > 4 ]--- > Software, Linux, Microcontrollers > http://www.brianlane.com > AIS Parser SDK > http://www.aisparser.com > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.8 (Darwin) > Comment: Remember Lexington Green! > > iD8DBQFI535RIftj/pcSws0RAldqAJ9yKYSyDArc/LZ6G47SwxUq4z8yAACgioyx > b9WnwDEQe8hSOuYbKuKo9sY= > =7lCV > -----END PGP SIGNATURE----- > _______________________________________________ > Tutor maillist - Tutor@... > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion programWhen I run it from the idle it works perfect, but when I run it from a
file I get none, why is that? >>> grades = [ (90,100,'A'), (80, 89,'B'), (70, 79,'C'), (60, 69,'D'), ( 0, 59,'F'), ] >>> score = 66 >>> def getGrade(score): """ Return a letter grade based on a score """ for g in grades: if (score <= g[1]) and (score >= g[0]): return g[2] >>> getGrade <function getGrade at 0x84ec80> >>> getGrade(score) 'D' >>> #!/usr/bin/python grades = [ (90,100,'A'), (80, 89,'B'), (70, 79,'C'), (60, 69,'D'), ( 0, 59,'F'), ] def getGrade(score): """ Return a letter grade based on a score """ for g in grades: if (score <= g[1]) and (score >= g[0]): return g[2] score = raw_input("What is your exam score: (0-100)? ") print getGrade print getGrade(score) What is your exam score: (0-100)? 66 <function getGrade at 0x2b4b2a310d70> None -- Have Fun, David A. Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion programOn Sat, Oct 4, 2008 at 10:31 AM, Brian C. Lane <bcl@...> wrote:
> for g in grades: > if (score <= g[1]) and (score >= g[0]): > return g[2] I think tuple unpacking makes code like this more readable: for lower, upper, grade in grades: if lower <= score <= upper: return grade Kent _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion programOn Sat, Oct 4, 2008 at 9:45 AM, Alan Gauld <alan.gauld@...> wrote:
> It's not too bad but I would probably use a dictionary rather > than the list - which avoids the index problem Not sure how the dict is better - in either case, leaving off the grade corresponding to a score of 100 will raise an exception. Kent _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
|
|
|
Re: bug in exam score conversion programOn Sat, Oct 4, 2008 at 12:55 PM, David <david@...> wrote:
> When I run it from the idle it works perfect, but when I run it from a > file I get none, why is that? > score = raw_input("What is your exam score: (0-100)? ") The value returned from raw_input() is a string; you have to convert it to an int: score = int(raw_input("What is your exam score: (0-100)? ")) Kent _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion programLots of good responses. And now for something completely different:
import string x = string.maketrans('567891', 'FDCBAA') score = raw_input('score>') print "Your grade is:", score[0].translate(x) -- Bob Gailer Chapel Hill NC 919-636-4239 When we take the time to be aware of our feelings and needs we have more satisfying interatctions with others. Nonviolent Communication provides tools for this awareness. As a coach and trainer I can assist you in learning this process. What is YOUR biggest relationship challenge? _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
|
|
|
Re: bug in exam score conversion programDragos Ionescu wrote:
> ----- Original Message ---- > From: bob gailer <bgailer@...> > To: David <ldl08@...> > Cc: tutor@... > Sent: Saturday, October 4, 2008 10:15:10 PM > Subject: Re: [Tutor] bug in exam score conversion program > > Lots of good responses. And now for something completely different: > > import string > x = string.maketrans('567891', 'FDCBAA') > score = raw_input('score>') > print "Your grade is:", score[0].translate(x) > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > > When we take the time to be aware of our feelings and > needs we have more satisfying interatctions with others. > > Nonviolent Communication provides tools for this awareness. > > As a coach and trainer I can assist you in learning this process. > > What is YOUR biggest relationship challenge? > > _______________________________________________ > Tutor maillist - Tutor@... <mailto:Tutor@...> > http://mail.python.org/mailman/listinfo/tutor > > > Wow! Bob Gailer's solution is so elegant. Can someone plese explain what > is the algorithm behind string.maketrans. More exactly, how is this > function doing the coding? Actually, I don't think the point was to be elegant as much as to get you thinking about something you might not have explored--never hurts to keep learning new features so you don't inefficiently apply the same old small set of things to new problems. You wouldn't *really* want to implement a production grade system like that, cute though it is. This is setting up a translation table mapping the first character in the score to a letter grade. So a 9 is changed to an A. The obvious problem though is how it handles a score of, say, "1". Or, for that matter, "37". > http://scripts.mit.edu/~dionescu/pyworld/ > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@... > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
|
|
|
Re: bug in exam score conversion programDragos Ionescu wrote:
> ---- Original Message ---- > From: Steve Willoughby <steve@...> > To: Dragos Ionescu <idragos@...> > Cc: bob gailer <bgailer@...>; David <ldl08@...>; tutor@... > Sent: Saturday, October 4, 2008 11:04:30 PM > Subject: Re: [Tutor] bug in exam score conversion program > > Dragos Ionescu wrote: > > ----- Original Message ---- > > From: bob gailer <bgailer@... <mailto:bgailer@...>> > > To: David <ldl08@... <mailto:ldl08@...>> > > Cc: tutor@... <mailto:tutor@...> > > Sent: Saturday, October 4, 2008 10:15:10 PM > > Subject: Re: [Tutor] bug in exam score conversion program > > > > Lots of good responses. And now for something completely different: > > > > import string > > x = string.maketrans('567891', 'FDCBAA') > > score = raw_input('score>') > > print "Your grade is:", score[0].translate(x) > > -- > > Bob Gailer > > Chapel Hill NC > > 919-636-4239 > > > > When we take the time to be aware of our feelings and > > needs we have more satisfying interatctions with others. > > > > Nonviolent Communication provides tools for this awareness. > > > > As a coach and trainer I can assist you in learning this process. > > > > What is YOUR biggest relationship challenge? > > > > _______________________________________________ > > Tutor maillist - Tutor@... <mailto:Tutor@...> > <mailto:Tutor@... <mailto:Tutor@...>> > > http://mail.python.org/mailman/listinfo/tutor > > > > > > Wow! Bob Gailer's solution is so elegant. Can someone plese explain what > > is the algorithm behind string.maketrans. More exactly, how is this > > function doing the coding? > > Actually, I don't think the point was to be elegant as much > as to get you thinking about something you might not have > explored--never hurts to keep learning new features so you > don't inefficiently apply the same old small set of things > to new problems. > > You wouldn't *really* want to implement a production grade > system like that, cute though it is. This is setting up a > translation table mapping the first character in the score > to a letter grade. So a 9 is changed to an A. The obvious > problem though is how it handles a score of, say, "1". Or, > for that matter, "37". > > > I know how string.maketrans works. I was wondering how to implement such > a function. Would that be very hard? I must admit that I was 'surprised' > when I printed x... How to implement... the equivalent of maketrans/translate? Pretty easy really. maketrans just builds a 256-byte table showing a mapping from one character set to another (compare perl's y/// or tr///). Once you have that translation table, all you really need to do is take each character of a string and make a new string by looking up each source character and returning what the table says (effectively table[ord(i)] for each character i in the source string). Which is pretty much what string.translate() is doing. or did I misunderstand which function you wanted to implement? _______________________________________________ Tutor maillist - Tutor@... http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: bug in exam score conversion program |