[Tutor] return values function thanks
Lea Parker
lea-parker at bigpond.com
Sat Apr 23 00:40:12 CEST 2011
Hi Alan
Thanks once again for your help.
Lea
-----Original Message-----
From: tutor-bounces+lea-parker=bigpond.com at python.org
[mailto:tutor-bounces+lea-parker=bigpond.com at python.org] On Behalf Of
tutor-request at python.org
Sent: Friday, 22 April 2011 5:32 PM
To: tutor at python.org
Subject: Tutor Digest, Vol 86, Issue 77
Send Tutor mailing list submissions to
tutor at python.org
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
tutor-request at python.org
You can reach the person managing the list at
tutor-owner at python.org
When replying, please edit your Subject line so it is more specific than
"Re: Contents of Tutor digest..."
Today's Topics:
1. Re: Jokes on Python Language (Marc Tompkins)
2. Re: learnpython.org - Free Interactive Python Tutorial
(Ron Reiter)
3. Re: Plotting a Linear Equation (Alan Gauld)
4. return values function (Lea Parker)
5. Re: return values function (Alan Gauld)
----------------------------------------------------------------------
Message: 1
Date: Thu, 21 Apr 2011 13:35:38 -0700
From: Marc Tompkins <marc.tompkins at gmail.com>
To: tutor at python.org
Subject: Re: [Tutor] Jokes on Python Language
Message-ID: <BANLkTinH4dDBSHb1t6nLodVqDEVxnDUJMw at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
On Thu, Apr 21, 2011 at 4:49 AM, Ratna Banjara <mast.ratna at gmail.com> wrote:
> Hello all,
>
> Does anybody knows jokes related to Python Language?
> If the answer is yes, please do share it...
>
Of course, there's the AWESOME webcomic xkcd (www.xkcd.com) which is
excellent reading even when he's not talking about Python... but in
particular, there are several great strips about Python itself (or at least,
they mention Python in a fond way):
http://xkcd.com/353/
http://xkcd.com/413/
http://xkcd.com/409/
http://xkcd.com/521/
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20110421/43418337/attach
ment-0001.html>
------------------------------
Message: 2
Date: Thu, 21 Apr 2011 20:22:39 +0300
From: Ron Reiter <ron.reiter at gmail.com>
To: tutor at python.org
Subject: Re: [Tutor] learnpython.org - Free Interactive Python
Tutorial
Message-ID: <BANLkTimCdLRFiBQ5CBqHLPCYZSbfThMZNA at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
For those of you who didn't notice the title, here is the link:
www.learnpython.org
Also, please note that there is still not enough content to actually learn
from the website yet. I am hoping people will help me with writing tutorials
so the site will be useful as soon as possible.
On Wed, Apr 20, 2011 at 9:16 PM, Ron Reiter <ron.reiter at gmail.com> wrote:
> Hey.
>
> I've created a website for learning Python interactively online. Check
> it out, and I would really appreciate it if you can also contribute
tutorials.
>
> Thanks!
>
> --
> -- Ron
>
--
-- Ron
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20110421/dad10c29/attach
ment-0001.html>
------------------------------
Message: 3
Date: Thu, 21 Apr 2011 22:40:51 +0100
From: "Alan Gauld" <alan.gauld at btinternet.com>
To: tutor at python.org
Subject: Re: [Tutor] Plotting a Linear Equation
Message-ID: <ioq891$6pq$1 at dough.gmane.org>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original
"Emanuel Woiski" <woiski at gmail.com> wrote
> easy:
I love your sense of humour!
> import pylab as pl
> import numpy as np
> x1,x2,n,m,b = 0.,10.,11,2.,5.
> x = np.r_[x1:x2:n*1j]
> pl.plot(x,m*x + b); pl.grid(); pl.show()
Now, given this is a list for beginners to Python, could you try explaining
what you did there and how the OP, or anyone else for that matter, might use
it?
Or were you really just trying to establish that if you try hard you can
write Python that is as difficult to read as Perl?
>> I want to use matplotlib (or similar) to plot an equation in
>> (y=mx+b) or standard form (Ax + By = C). I could make two points
>> out of those manually, but I was wondering if anyone knew of an
>> easier way. Thanks.
Alan G.
------------------------------
Message: 4
Date: Fri, 22 Apr 2011 16:59:18 +1000
From: "Lea Parker" <lea-parker at bigpond.com>
To: <tutor at python.org>
Subject: [Tutor] return values function
Message-ID: <000301cc00ba$ccffb950$66ff2bf0$@bigpond.com>
Content-Type: text/plain; charset="us-ascii"
Hello
I am hoping someone can put me on the right track. The code below includes
the assignment question at the beginning.
I seem to have been able to calculate average ok, but what I can't seem to
do is sort it so it will return a grade for each result.
Can you give me some advice to head me in the right direction please. My
code is:
"""Write a program that asks the user to enter 5 sets tests scores. The
program
should then display the 'letter grade' (A, B, C, D, F) for each test score,
and
the overall average test schore. Write the following functions in the
program:
* Calc_average: This function should take five test scores as parameters,
and
return the average score.
*determine_grade; this function should take a single test score as a
parameter,
and return a letter grade for the test. The letter grade should be on the
following grade scale: 90-100: A, 80-89: B, 70-79: C, 60-69: D, <60: F."""
def main():
#Get users first test result
test1 = int(raw_input('Enter the score for test 1: '))
#Get users second test result
test2 = int(raw_input('Enter the score for test 2: '))
#Get users third test result
test3 = int(raw_input('Enter the score for test 3: '))
#Get users forth test result
test4 = int(raw_input('Enter the score for test 4: '))
#Get users fifth test result
test5 = int(raw_input('Enter the score for test 5: '))
#Get the sum of the test results
cal_average = sum(test1, test2, test3, test4, test5)/5
#Display the total of tests
print 'Together your tests average is: ', cal_average
print 'Your grade is: ', grade
# The sum function to total all tests
def sum(test1, test2, test3, test4, test5):
result = test1 + test2 + test3 + test4 + test5
return result
def determine_grade(score):
#Determine the grade for each score
if score <101 and score >89:
score = A
elif score <90 and score >79:
score = B
elif score <80 and score >69:
score = C
elif score <70 and score >59:
score = D
else:
score = F
return score
# Call the main function
main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20110422/fede14a5/attach
ment-0001.html>
------------------------------
Message: 5
Date: Fri, 22 Apr 2011 08:32:04 +0100
From: "Alan Gauld" <alan.gauld at btinternet.com>
To: tutor at python.org
Subject: Re: [Tutor] return values function
Message-ID: <iorati$7et$1 at dough.gmane.org>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original
"Lea Parker" <lea-parker at bigpond.com> wrote
> Write the following functions in the
> program:
>
> * Calc_average: This function should take five test scores as
> parameters, and return the average score.
Note that you are supposed to write a function to do this not just do it
inline.
> *determine_grade; this function should take a single test score as a
> parameter, and return a letter grade for the test.
> The letter grade should be on the following grade scale:
> 90-100: A, 80-89: B, 70-79: C, 60-69: D, <60: F."""
>
> def main():
> #Get users first test result
> test1 = int(raw_input('Enter the score for test 1: '))
> test2 = int(raw_input('Enter the score for test 2: '))
> test3 = int(raw_input('Enter the score for test 3: '))
> test4 = int(raw_input('Enter the score for test 4: '))
> test5 = int(raw_input('Enter the score for test 5: '))
Every time you see repeating code like that you should ask, can I use a
loop? If you store the results in a list, it then becomes a simple job to
read 5 results:
for n in range(5):
results.append( int(raw_input('Enter the score for test %d:" %
n')) )
> #Get the sum of the test results
> cal_average = sum(test1, test2, test3, test4, test5)/5
This is the average not the sum so your comment is wrong.
It's also where you should be using the function you were asked to define.
The function body would look like the line above.
> print 'Together your tests average is: ', cal_average
> print 'Your grade is: ', grade
And here you need to call your other function to work out the grade.
But again you need to call it for each score. You can repeat it
5 times as you did for raw_input or you could use the loop; method I showed
you above.
> # The sum function to total all tests def sum(test1, test2, test3,
> test4, test5):
> result = test1 + test2 + test3 + test4 + test5
> return result
You didn't need this because Python already has a sum() function that will
do this for you.
> def determine_grade(score):
> #Determine the grade for each score
> if score <101 and score >89:
> score = A
> elif score <90 and score >79:
> score = B
> elif score <80 and score >69:
> score = C
> elif score <70 and score >59:
> score = D
> else:
> score = F
> return score
The most serious problem, and it should have thrown an error, is that the
results need to be strings. Otherwise Python will look for a variable called
A,B,C etc.
And no such name exists in your code.
Its not a good idea to use score for both the input parameter that you test
and the result that you return. In this case you should get away with it
because you never use score after you set it but in general you run the risk
of losing access to the input value after you assign a result to score.
In the comparisons Python allows you to write them like
if 101 > score > 89: grade = "A"
which is slightly easier to type and read.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
------------------------------
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
End of Tutor Digest, Vol 86, Issue 77
*************************************
More information about the Tutor
mailing list