Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld
"Dragos Ionescu" <[EMAIL PROTECTED]> wrote David try this: score = input("What is your exam score: (0-100)? ") No, please don't! input has several security issues, it is much better to use raw_input but convert the result to the type you need: score = int(raw_input("What is your exam sc

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld
"David" <[EMAIL PROTECTED]> 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 = 66 Here you directly assign a number to score #!/usr/bin/python score = raw_input("What is your exam score: (0-100)? ") Here you assign a str

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> 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. Sure, you co

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Steve Willoughby
utor@python.org> > 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

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Dragos Ionescu
Original Message From: Steve Willoughby <[EMAIL PROTECTED]> To: Dragos Ionescu <[EMAIL PROTECTED]> Cc: bob gailer <[EMAIL PROTECTED]>; David <[EMAIL PROTECTED]>; tutor@python.org Sent: Saturday, October 4, 2008 11:04:30 PM Subject: Re: [Tutor] bug in exam score co

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Steve Willoughby
Dragos Ionescu wrote: - Original Message From: bob gailer <[EMAIL PROTECTED]> To: David <[EMAIL PROTECTED]> Cc: tutor@python.org Sent: Saturday, October 4, 2008 10:15:10 PM Subject: Re: [Tutor] bug in exam score conversion program Lots of good responses. And now f

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Dragos Ionescu
- Original Message From: bob gailer <[EMAIL PROTECTED]> To: David <[EMAIL PROTECTED]> Cc: tutor@python.org 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 differ

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread bob gailer
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

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Dragos Ionescu
tutor@python.org Sent: Saturday, October 4, 2008 7:55:57 PM Subject: Re: [Tutor] bug in exam score conversion program When 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'

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Kent Johnson
On Sat, Oct 4, 2008 at 12:55 PM, David <[EMAIL PROTECTED]> 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

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread David
When 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): """

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Kent Johnson
On Sat, Oct 4, 2008 at 9:45 AM, Alan Gauld <[EMAIL PROTECTED]> 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

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Kent Johnson
On Sat, Oct 4, 2008 at 10:31 AM, Brian C. Lane <[EMAIL PROTECTED]> 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:

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld
"Brian C. Lane" <[EMAIL PROTECTED]> 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:

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Brian C. Lane
-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

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld
"David" <[EMAIL PROTECTED]> 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

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread W W
On Sat, Oct 4, 2008 at 5:11 AM, David <[EMAIL PROTECTED]> wrote: > Hello!! 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

Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Sander Sweers
On Sat, Oct 4, 2008 at 12:11, David <[EMAIL PROTECTED]> 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 figu

[Tutor] bug in exam score conversion program

2008-10-04 Thread David
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." I am quite happy