Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108560 > unrolled thread
| Started by | kobsx4@gmail.com |
|---|---|
| First post | 2016-05-12 08:47 -0700 |
| Last post | 2016-05-13 12:17 -0400 |
| Articles | 16 — 10 participants |
Back to article view | Back to comp.lang.python
Average calculation Program *need help* kobsx4@gmail.com - 2016-05-12 08:47 -0700
Re: Average calculation Program *need help* Rustom Mody <rustompmody@gmail.com> - 2016-05-12 09:01 -0700
Re: Average calculation Program *need help* Igor Korot <ikorot01@gmail.com> - 2016-05-12 12:06 -0400
Re: Average calculation Program *need help* DFS <nospam@dfs.com> - 2016-05-12 19:45 -0400
Re: Average calculation Program *need help* "Martin A. Brown" <martin@linux-ip.net> - 2016-05-12 09:13 -0700
Re: Average calculation Program *need help* MRAB <python@mrabarnett.plus.com> - 2016-05-12 17:14 +0100
Re: Average calculation Program *need help* Jake Kobs <kobsx4@gmail.com> - 2016-05-12 21:22 -0700
Re: Average calculation Program *need help* Ben Finney <ben+python@benfinney.id.au> - 2016-05-13 14:55 +1000
Re: Average calculation Program *need help* Michael Torrie <torriem@gmail.com> - 2016-05-12 22:57 -0600
Re: Average calculation Program *need help* Jake Kobs <kobsx4@gmail.com> - 2016-05-12 22:03 -0700
Re: Average calculation Program *need help* Michael Torrie <torriem@gmail.com> - 2016-05-12 23:29 -0600
Re: Average calculation Program *need help* DFS <nospam@dfs.com> - 2016-05-13 01:12 -0400
Re: Average calculation Program *need help* Jake Kobs <kobsx4@gmail.com> - 2016-05-12 22:46 -0700
Re: Average calculation Program *need help* Nick Sarbicki <nick.a.sarbicki@gmail.com> - 2016-05-13 07:10 +0000
Re: Average calculation Program *need help* Jake Kobs <kobsx4@gmail.com> - 2016-05-13 08:38 -0700
Re: Average calculation Program *need help* DFS <nospam@dfs.com> - 2016-05-13 12:17 -0400
| From | kobsx4@gmail.com |
|---|---|
| Date | 2016-05-12 08:47 -0700 |
| Subject | Average calculation Program *need help* |
| Message-ID | <011a2f60-aafd-484f-8cb2-6017aaf1bb29@googlegroups.com> |
Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
------------------------------------------------------------------------------
#this function will get the total scores
def getScores(totalScores, number):
for counter in range(0, number):
score = input('Enter their score: ')
totalScores = totalScores + score
while not (score >= 0 and score <= 100):
print "Your score must be between 0 and 100."
score = input('Enter their score: ')
return totalScores
------------------------------------------------------------------------------
the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
[toc] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-05-12 09:01 -0700 |
| Message-ID | <42f1887b-f846-4255-a73b-00593339222c@googlegroups.com> |
| In reply to | #108560 |
On Thursday, May 12, 2016 at 9:18:08 PM UTC+5:30, Jake Kobs wrote:
> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
> ------------------------------------------------------------------------------
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
>
> while not (score >= 0 and score <= 100):
>
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
>
>
>
> return totalScores
> ------------------------------------------------------------------------------
> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
I suggest you conceptualize the problem into 3 sub-problems:
- IO
- average computation
- validation (between 0 and 100)
For starters ignore validation.
So we only have IO + computation
ie Input then computation then Output
Now if your input needs to feed into a computation you need a *data-structure*
I suggest you consider lists
eg the 5 scores
4
2
1
3
5
would be represented as the list [4,2,1,3,5]
Their average -- 3 -- is to be *computed* and then to be given back to the user
-- ie *output*
Does this help start you off?
[toc] | [prev] | [next] | [standalone]
| From | Igor Korot <ikorot01@gmail.com> |
|---|---|
| Date | 2016-05-12 12:06 -0400 |
| Message-ID | <mailman.613.1463069194.32212.python-list@python.org> |
| In reply to | #108560 |
Hi,
On Thu, May 12, 2016 at 11:47 AM, <kobsx4@gmail.com> wrote:
> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
> ------------------------------------------------------------------------------
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
>
> while not (score >= 0 and score <= 100):
>
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
>
>
>
> return totalScores
> ------------------------------------------------------------------------------
> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
Can you solve this problem with just pen and paper?
Do that and then translate the algorithm to python.
Thank you.
> --
> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2016-05-12 19:45 -0400 |
| Message-ID | <nh34c6$5rs$1@dont-email.me> |
| In reply to | #108562 |
On 5/12/2016 12:06 PM, Igor Korot wrote:
> Hi,
>
> On Thu, May 12, 2016 at 11:47 AM, <kobsx4@gmail.com> wrote:
>> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
>> ------------------------------------------------------------------------------
>> #this function will get the total scores
>> def getScores(totalScores, number):
>> for counter in range(0, number):
>> score = input('Enter their score: ')
>> totalScores = totalScores + score
>>
>> while not (score >= 0 and score <= 100):
>>
>> print "Your score must be between 0 and 100."
>> score = input('Enter their score: ')
>>
>>
>>
>> return totalScores
>> ------------------------------------------------------------------------------
>> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
>
> Can you solve this problem with just pen and paper?
> Do that and then translate the algorithm to python.
"It’s very simple — you read the protocol and write the code."
https://www.salon.com/2000/05/16/chapter_2_part_one/
[toc] | [prev] | [next] | [standalone]
| From | "Martin A. Brown" <martin@linux-ip.net> |
|---|---|
| Date | 2016-05-12 09:13 -0700 |
| Message-ID | <mailman.614.1463069649.32212.python-list@python.org> |
| In reply to | #108560 |
Greetings kobsx4,
>Hello all, I have been struggling with this code for 3 hours now
>and I'm still stumped. My problem is that when I run the following
>code:
>------------------------------------------------------------------------------
>#this function will get the total scores
>def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
>
> while not (score >= 0 and score <= 100):
>
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
>
>
>
> return totalScores
>------------------------------------------------------------------------------
>the program is supposed to find the average of two test scores and
>if one of the scores is out of the score range (0-100), an error
>message is displayed. The main problem with this is that when
>someone types in a number outside of the range, it'll ask them to
>enter two scores again, but ends up adding all of the scores
>together (including the invalid ones) and dividing by how many
>there are. Please help.
Suggestion #1:
--------------
When you are stuck on a small piece of code, set it aside (stop
looking at it) and start over again; sometimes rewriting with
different variable names and a clean slate helps to highlight the
problem.
Professional programmers will tell you that they are quite
accustomed to 'throwing away' code. Don't be afraid to do it.
(While you are still learning, you might want to keep the old chunk
of code around to examine so that you can maybe figure out what you
did wrong.)
Suggestion #2:
--------------
Put a print statement or two in the loop, so that you see how your
variables are changing. For example, just before your 'while' line,
maybe something like:
print "score=%d totalScores=%d" % (score, totalScores,)
Suggestion #3:
--------------
Break the problem down even smaller (Rustom Mody appears to have
beat me to the punch on that suggestion, so I'll just point to his
email.)
Hint #1:
--------
What is the value of your variable totalScores each time through the
loop? Does it ever get reset?
Good luck with your degubbing!
-Martin
--
Martin A. Brown
http://linux-ip.net/
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-05-12 17:14 +0100 |
| Message-ID | <mailman.615.1463069701.32212.python-list@python.org> |
| In reply to | #108560 |
On 2016-05-12 16:47, kobsx4@gmail.com wrote:
> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
> ------------------------------------------------------------------------------
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
>
> while not (score >= 0 and score <= 100):
>
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
>
>
>
> return totalScores
> ------------------------------------------------------------------------------
> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
>
Basically what you want is:
repeat as many times as there are scores you want:
ask for the score
while it's invalid:
ask for it again
add it to the total
What you're actually doing is asking for the scores and adding them
_before_ checking. Don't include something before you've checked that
it's OK.
[toc] | [prev] | [next] | [standalone]
| From | Jake Kobs <kobsx4@gmail.com> |
|---|---|
| Date | 2016-05-12 21:22 -0700 |
| Message-ID | <dab38f98-4ba3-4673-bfb3-e2aa676c9110@googlegroups.com> |
| In reply to | #108560 |
On Thursday, May 12, 2016 at 10:48:08 AM UTC-5, Jake Kobs wrote:
> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
> ------------------------------------------------------------------------------
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
>
> while not (score >= 0 and score <= 100):
>
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
>
>
>
> return totalScores
> ------------------------------------------------------------------------------
> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
I still can't get it. Someone please tell me lol. I have done everything I can and still I get bad answers.
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-05-13 14:55 +1000 |
| Message-ID | <mailman.620.1463115329.32212.python-list@python.org> |
| In reply to | #108573 |
Jake Kobs <kobsx4@gmail.com> writes: > I still can't get it. Someone please tell me lol. I have done > everything I can and still I get bad answers. You may want to join our dedicated beginner tutoring forum <URL:https://mail.python.org/mailman/listinfo/tutor>, which specialises in collaborative teaching of the fundamentals of Python. -- \ “If we don't believe in freedom of expression for people we | `\ despise, we don't believe in it at all.” —Noam Chomsky, | _o__) 1992-11-25 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2016-05-12 22:57 -0600 |
| Message-ID | <mailman.621.1463115436.32212.python-list@python.org> |
| In reply to | #108573 |
On 05/12/2016 10:22 PM, Jake Kobs wrote:
> On Thursday, May 12, 2016 at 10:48:08 AM UTC-5, Jake Kobs wrote:
>> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
>> ------------------------------------------------------------------------------
>> #this function will get the total scores
>> def getScores(totalScores, number):
>> for counter in range(0, number):
>> score = input('Enter their score: ')
>> totalScores = totalScores + score
>>
>> while not (score >= 0 and score <= 100):
>>
>> print "Your score must be between 0 and 100."
>> score = input('Enter their score: ')
>>
>>
>>
>> return totalScores
>> ------------------------------------------------------------------------------
>> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
>
> I still can't get it. Someone please tell me lol. I have done everything I can and still I get bad answers.
Tell us what you've done, precisely. We're here to help you learn, not
give you the answers.
One thing that strikes me is that your while loop doesn't appear to be
indented properly. You have it running after all the scores have been
inputted in the for loop, but I suspect this isn't what you want. You
want the while loop to occur each time through the for loop. Do you
know how to move this while loop to be inside the for loop?
[toc] | [prev] | [next] | [standalone]
| From | Jake Kobs <kobsx4@gmail.com> |
|---|---|
| Date | 2016-05-12 22:03 -0700 |
| Message-ID | <26884493-24d4-4d3d-b7d8-816f043b28b7@googlegroups.com> |
| In reply to | #108575 |
On Thursday, May 12, 2016 at 11:57:28 PM UTC-5, Michael Torrie wrote:
> On 05/12/2016 10:22 PM, Jake Kobs wrote:
> > On Thursday, May 12, 2016 at 10:48:08 AM UTC-5, Jake Kobs wrote:
> >> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
> >> ------------------------------------------------------------------------------
> >> #this function will get the total scores
> >> def getScores(totalScores, number):
> >> for counter in range(0, number):
> >> score = input('Enter their score: ')
> >> totalScores = totalScores + score
> >>
> >> while not (score >= 0 and score <= 100):
> >>
> >> print "Your score must be between 0 and 100."
> >> score = input('Enter their score: ')
> >>
> >>
> >>
> >> return totalScores
> >> ------------------------------------------------------------------------------
> >> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
> >
> > I still can't get it. Someone please tell me lol. I have done everything I can and still I get bad answers.
>
> Tell us what you've done, precisely. We're here to help you learn, not
> give you the answers.
>
> One thing that strikes me is that your while loop doesn't appear to be
> indented properly. You have it running after all the scores have been
> inputted in the for loop, but I suspect this isn't what you want. You
> want the while loop to occur each time through the for loop. Do you
> know how to move this while loop to be inside the for loop?
Im not sure how to move it inside the for loop. I've been working on this small problem for like 4 hours lol.
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2016-05-12 23:29 -0600 |
| Message-ID | <mailman.625.1463117393.32212.python-list@python.org> |
| In reply to | #108576 |
On 05/12/2016 11:03 PM, Jake Kobs wrote:
> Im not sure how to move it inside the for loop. I've been working on
> this small problem for like 4 hours lol.
I'm sorry it's so frustrating. Sounds like you haven't got down some of
the most basic fundamentals yet. In Python, things that should happen
one after another are placed at the same indent level on the screen.
For example:
if something:
do_this()
then_this()
then_that()
Everything indented in from the start of the if statement is in a block
and only happens if the if statement condition is true. If there're
statements at the same level as the if, then they happen *after* the if
and it's block. In other words, indentation is what tells Python where
you want things to run. If you want to move the while loop inside the
for loop, you have to adjust it's indentation accordingly (and the
indentation in it's own block).
This is a big gotcha for people unfamiliar with programming in Python,
and unfamiliar with programming in general. In other languages, indents
don't have to be a certain way, as long as you have the write statements
to close off the loop. However, Python's method forces you to think
like a programmer and to lay things out on the screen in a logical
fashion, like a writer's outline.
If you're still stuck, you will probably want to sit down with your
teacher and have him or her go over this with you. This is important
basic stuff you need to have clear in your mind to program computers.
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2016-05-13 01:12 -0400 |
| Message-ID | <nh3nhf$mfd$1@dont-email.me> |
| In reply to | #108573 |
On 5/13/2016 12:22 AM, Jake Kobs wrote:
> On Thursday, May 12, 2016 at 10:48:08 AM UTC-5, Jake Kobs wrote:
>> Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code:
>> ------------------------------------------------------------------------------
>> #this function will get the total scores
>> def getScores(totalScores, number):
>> for counter in range(0, number):
>> score = input('Enter their score: ')
>> totalScores = totalScores + score
>>
>> while not (score >= 0 and score <= 100):
>>
>> print "Your score must be between 0 and 100."
>> score = input('Enter their score: ')
>>
>>
>>
>> return totalScores
>> ------------------------------------------------------------------------------
>> the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help.
>
> I still can't get it. Someone please tell me lol. I have done everything I can and still I get bad answers.
This seems to work, but I'm new to python and it's probably kludgey.
It will get the avg of however many numbers you enter.
-----------------------------------------------------------
#calcAvg.py
import sys
def getScores(number):
i, totalScores = 0,0
while 1:
score = input('Enter score: ')
if (score < 0 or score > 100):
print "Score must be between 0 and 100."
else:
totalScores = totalScores + score
i += 1
if i == number:
print "Avg of %s scores is %s" % (number, totalScores/number)
break
nbr = sys.argv[1]
getScore(nbr)
-----------------------------------------------------------
$ python calcAvg.py 3
Enter score: 101
Score must be between 0 and 100.
Enter score: 3
Enter score: 4
Enter score: 5
Avg of 3 scores is 4
$ python calcAvg.py 2
Enter score: 50
Enter score: 60
Avg of 2 scores is 55
[toc] | [prev] | [next] | [standalone]
| From | Jake Kobs <kobsx4@gmail.com> |
|---|---|
| Date | 2016-05-12 22:46 -0700 |
| Message-ID | <25510084-74ff-4f7f-b99f-bb29bc675899@googlegroups.com> |
| In reply to | #108560 |
Thank you for the help..I think I'm getting closer, but I feel like after they enter an invalid number, it should reset the invalid number(s) somehow. Here's my updated code:
------------------------------------------------------------------------------
#this function will get the total scores
def getScores(totalScores, number):
for counter in range(0, number):
score = input('Enter their score: ')
if (score < 100 and score > 0):
totalScores = totalScores + score
while (score > 100 or score < 0):
print "Your scores must be between 0 and 100."
score = input('Enter their score: ')
score = input('Enter their score: ')
totalScores = totalScores + score
return totalScores
----------------------------------------------------------------------------
[toc] | [prev] | [next] | [standalone]
| From | Nick Sarbicki <nick.a.sarbicki@gmail.com> |
|---|---|
| Date | 2016-05-13 07:10 +0000 |
| Message-ID | <mailman.626.1463123428.32212.python-list@python.org> |
| In reply to | #108583 |
On Fri, 13 May 2016, 06:52 Jake Kobs, <kobsx4@gmail.com> wrote:
> Thank you for the help..I think I'm getting closer, but I feel like after
> they enter an invalid number, it should reset the invalid number(s)
> somehow. Here's my updated code:
>
> ------------------------------------------------------------------------------
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> if (score < 100 and score > 0):
> totalScores = totalScores + score
> while (score > 100 or score < 0):
>
> print "Your scores must be between 0 and 100."
> score = input('Enter their score: ')
> score = input('Enter their score: ')
> totalScores = totalScores + score
>
>
> return totalScores
>
> ----------------------------------------------------------------------------
> --
>
You're starting to get it which is good.
The if is a good idea to begin with, but your while loop still executes
after the for loop finishes as it is not properly indented.
As Michael said, indentation indicates what statement has ownership of a
block. Currently your while loop has it's own block outside the for loop.
This means it gets executed after your for loop has finished looping
through your range. You want the while to be held within the for loop so it
gets executed immediately after each input statement.
Think. Your if is inside the for loop and gets executed after every input.
Your input is inside the for loop. Your while and it's contents are not in
the for loop and get executed after the for loop finishes. The only
difference that matters here is indentation.
Try solve that bit first.
>
[toc] | [prev] | [next] | [standalone]
| From | Jake Kobs <kobsx4@gmail.com> |
|---|---|
| Date | 2016-05-13 08:38 -0700 |
| Message-ID | <13a4ef84-6cc8-4f7d-a615-75bd183e3d93@googlegroups.com> |
| In reply to | #108587 |
Thank you so much! I finally got it. :)
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2016-05-13 12:17 -0400 |
| Message-ID | <nh4uga$tgv$1@dont-email.me> |
| In reply to | #108595 |
On 5/13/2016 11:38 AM, Jake Kobs wrote: > Thank you so much! I finally got it. :) What does it look like?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web