1
\$\begingroup\$

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")
\$\endgroup\$
2
  • 1
    \$\begingroup\$ have you read the assignment? excluding -99. Also you are displaying results after every input. \$\endgroup\$
    – Imre L
    Commented Nov 27, 2013 at 16:30
  • 1
    \$\begingroup\$ You should mark an answer as accepted if you think it answers your question :) \$\endgroup\$
    – Max
    Commented Dec 5, 2013 at 10:39

2 Answers 2

4
\$\begingroup\$

A few things:

The application should do 2 things.

  1. Collect up to 10 different integers, or until the entered value is -99

  2. 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 do a = 1
  • You usually iterate an array (or in Pythons case a list) from 0 to N, and not 1 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 to inputNr. 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.
\$\endgroup\$
2
  • 2
    \$\begingroup\$ I would remove the unused j and enumerate, leaving just for val in numbers: sum += val. Or, better, use sum() for the whole thing. \$\endgroup\$ Commented Nov 28, 2013 at 2:56
  • \$\begingroup\$ @MichaelUrman Good point! I'm still new to Python... \$\endgroup\$
    – Max
    Commented Nov 28, 2013 at 7:51
2
\$\begingroup\$

A few things that spring to mind:

  1. 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.
  2. 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.
  3. 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 division s * 1. / i .
  4. You could use string substitution to format your output in a nice way. See here in the docs.
  5. break is required but continue is not. You should read continue 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 after continue, 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)
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Given the python3 tag, points 3 and 4 are not necessary here. Still they're good reference for python2 users. \$\endgroup\$ Commented 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
    Commented Nov 28, 2013 at 15:38
  • 2
    \$\begingroup\$ your code doesn't properly return the average \$\endgroup\$
    – Malachi
    Commented Nov 29, 2013 at 4:51
  • 1
    \$\begingroup\$ @Malachi - Apologies, the -99 case should have subtracted 1 from i. Fixed now. \$\endgroup\$
    – ejrb
    Commented Dec 5, 2013 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.