Dear list,
one of the exercises in Zelle's book is:
given
import string
s1 = "spam"
s2 = "ni!"
show a Python expression that could construct the following result by
performing string operations on s1 and s2:
"Spam Ni! Spam Ni! Spam Ni!".
I have two solutions:
a)
(string.capitalize(s1) + "
On Oct 4, 2008, at 4:03 AM, David wrote:
Dear list,
one of the exercises in Zelle's book is:
given
import string
s1 = "spam"
s2 = "ni!"
show a Python expression that could construct the following result
by performing string operations on s1 and s2:
"Spam Ni! Spam Ni! Spam Ni!".
I have t
"David" <[EMAIL PROTECTED]> wrote
I have two solutions:
a)
(string.capitalize(s1) + " " + string.capitalize( s2) + " " ) * 3
b)
"%s %s " % (string.capitalize(s1), string.capitalize(s2)) * 3
Both seem to work, but they seem overly complex. Where could I
improve?
Personally I'd go with b bu
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
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
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
On Sat, Oct 4, 2008 at 4:03 AM, David <[EMAIL PROTECTED]> wrote:
> Dear list,
>
> one of the exercises in Zelle's book is:
>
> given
>
> import string
> s1 = "spam"
> s2 = "ni!"
>
> show a Python expression that could construct the following result by
> performing string operations on s1 and s2:
N
On Fri, Oct 3, 2008 at 4:04 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:
> I would try explicitly converting the data to latin-1 before you send
> it to the database, giving it one of the forgiving error handling
> methods I referred to earlier. Or, change your database to UTF-8...
BTW to create a
On Fri, Oct 3, 2008 at 8:31 PM, <[EMAIL PROTECTED]> wrote:
> I have a Tkinter button widget that when pressed invokes a Toplevel window
> call each time. The Toplevel window thus generated has a close button on it.
> As you might guess, when multiple Toplevel windows are open, I can press on a
<[EMAIL PROTECTED]> wrote
The behavior I seek is that one and only one Toplevel window
gets generated no matter how many times the original Tkinter
button is pressed.
Here is a minimal example of what I think you want?
Does that help?
--
Alan Gauld
Author of the Learn to Program web site
ht
"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
-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
"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:
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:
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
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):
"""
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
David try this:
score = input("What is your exam score: (0-100)? ")
print getGrade
print getGrade(score)
Regards,
Dragoshttp://scripts.mit.edu/~dionescu/pyworld/
- Original Message
From: David <[EMAIL PROTECTED]>
To: Brian C. Lane <[EMAIL PROTECTED]>
Cc: tutor@python.org
Sent: Satu
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
- 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 different:
import stri
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 for something completely
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 conversion program
Dragos
Dragos Ionescu wrote:
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 con
(Or you could start using Gmail, which conveniantly archives Qs with their
As, based on subject line,)
On Wed, Oct 1, 2008 at 12:26 AM, Alan Gauld <[EMAIL PROTECTED]>wrote:
>
> "Danny Yoo" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>
>> Yup. See:
>>
>> http://mail.python.org
You could check out Google Visualization API. supposed to be fairly nifty;
haven't had time to check it out myself though.
On Wed, Oct 1, 2008 at 4:49 PM, R. Alan Monroe <[EMAIL PROTECTED]>wrote:
> > Is there a text graphics module that does say scatter plots or
> > histograms? I'm thinking of st
"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
"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
"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
Jaggo wrote:
So, am I to understand from this lack of response there be *no
particular reason* to use Temp file?
AFAIC there is no particular reason to use Temp file. It is a
convenience for those of us who need temp files and don't want to bother
creating unique names.
--
Bob Gailer
Chapel
29 matches
Mail list logo