I have an assignment as follows:
Write a program where you can enter from the keyboard up to 10 integers. If the number entered is equal to -99, stop reading numbers from the keyboard and compute the average and sum of all values (excluding -99). Print the average on the screen. Make sure you consider all the cases: less than 10 or exactly 10 numbers can be entered. After the numbers have been entered you need to make sure that the average is computed then.
I came up with the program below. Do you think my program is fine? What could be alternative solutions?
s = 0
for i in range(1, 11):
a=int(input("Enter a number: "))
if a==-99:
s = s+a
print("The sum of the numbers that you've entered excluding -99:",s-a) # minus a because we want to exclude -99
print("The average of the numbers that you've entered excluding -99:",(s-a)/(i-1)) # minus a because we want to exclude -99. i-1 in denominator because we excluded -99 from the sum.
break
else:
s = s+a
print("The sum of the number(s) that you've entered:",s)
print("The average of the number(s) that you've entered:",s/i)
continue
input("Press enter to close")
2 Answers 2
A few things:
The application should do 2 things.
Collect up to 10 different integers, or until the entered value is -99
Sum and Average the values
# so lets start by creating a list and fill it with all the numbers we need
numbers = list()
for i in range(0, 10):
inputNr = int(input("Enter a number: "))
if(inputNr == -99):
break;
numbers.append(inputNr)
#Then we take all of the numbers and calculate the sum and avg on them
sum = 0
for j, val in enumerate(numbers):
sum += val
print("The total sum is: " + str(sum))
print("The avg is: " + str(sum / len(numbers)))
A few pointers about your code:
- Use a whitespace (a single space) between variable declaration and assignment. Where you did
a=1
you want to doa = 1
- You usually iterate an array (or in Pythons case a list) from
0 to N
, and not1 to N
- Create variables which you can reuse. As you can see in my example I create a
inputNr
variable which holds the input. I later reuse this to check if it's-99
and I reuse it by adding it to my list - variable names, function names, and everything else should have a descriptive and concise name. Where you named your input variable to
a
I named mine toinputNr
. This means that when we later reuse this variable in the code it will be clear to us what it is and what it holds.
-
2\$\begingroup\$ I would remove the unused
j
andenumerate
, leaving justfor val in numbers: sum += val
. Or, better, usesum()
for the whole thing. \$\endgroup\$Michael Urman– Michael Urman2013年11月28日 02:56:18 +00:00Commented Nov 28, 2013 at 2:56 -
\$\begingroup\$ @MichaelUrman Good point! I'm still new to Python... \$\endgroup\$Max– Max2013年11月28日 07:51:43 +00:00Commented Nov 28, 2013 at 7:51
A few things that spring to mind:
- Keeping a running total is a good idea but take care to catch the
-99
case - there's no need to add it to the total at all.- I like how you're using range to count the inputs. This, plus the first point means you don't have to store the actual inputs anywhere.
- My personal oppinion: Your division is fine but would not work in python 2.x. I prefer explicit integer/float division to avoid this ambiguity. You could start with
s
as a float,s = 0.
or force float divisions * 1. / i
.- You could use string substitution to format your output in a nice way. See here in the docs.
break
is required butcontinue
is not. You should readcontinue
as "continue to next loop iteration, do not execute any more code until you get there". As there is no more code in the loop aftercontinue
, it is not needed.
Here's my version:
s = 0 # Ok in python 3, for 2.x explicitly use a float s = 0.
for i in range(1, 11):
n = int(input()) # Careful to only enter numbers here
if n == -99:
i -= 1
break
s += n
i = i if i else 1 # Avoids ZeroDivisionError
print 'The sum was: {}\nThe average was: {}'.format(s, s/i)
-
1\$\begingroup\$ Given the
python3
tag, points 3 and 4 are not necessary here. Still they're good reference forpython2
users. \$\endgroup\$Michael Urman– Michael Urman2013年11月28日 14:19:01 +00:00Commented Nov 28, 2013 at 14:19 -
1\$\begingroup\$ thanks MichaelUrman, I didn't notice that tag. I'll edit the post to take it into account \$\endgroup\$ejrb– ejrb2013年11月28日 15:38:19 +00:00Commented Nov 28, 2013 at 15:38
-
2\$\begingroup\$ your code doesn't properly return the average \$\endgroup\$Malachi– Malachi2013年11月29日 04:51:14 +00:00Commented Nov 29, 2013 at 4:51
-
1\$\begingroup\$ @Malachi - Apologies, the -99 case should have subtracted 1 from i. Fixed now. \$\endgroup\$ejrb– ejrb2013年12月05日 10:33:51 +00:00Commented Dec 5, 2013 at 10:33
excluding -99
. Also you are displaying results after every input. \$\endgroup\$