3

I'm working on a homework assignment for my introductory python programming class and I am stuck on a problem. The instructions are to:

Modify the find_sum() function so that it prints the average of the values entered. Unlike the average() function from before, we can’t use the len() function to find the length of the sequence; instead, you’ll have to introduce another variable to “count” the values as they are entered.

I am unsure on how to count the number of inputs and if anyone can give me a good starting point, that would be great!

# Finds the total of a sequence of numbers entered by user 
def find_sum(): 
     total = 0 
     entry = raw_input("Enter a value, or q to quit: ") 
     while entry != "q": 
         total += int(entry) 
         entry = raw_input("Enter a value, or q to quit: ") 
     print "The total is", total 
Bart
  • 19,692
  • 7
  • 68
  • 77
Lizi Wiki
  • 33
  • 1
  • 1
  • 4

2 Answers2

3

Every time you read an input total += int(entry), immediately afterward you should increment a variable.

num += 1 is all it would take, after you've initialized it to 0 elsewhere.

Make sure your indentation level is the same for all statements in the while loop. Your post (as originally written) did not reflect any indentation.

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
  • Wow, that was so simple. I was definitely overthinking it. Thank you for all your help and I will definite fix the indentation! – Lizi Wiki Mar 06 '13 at 03:36
  • Glad I could help. Some friendly editor seems to have fixed your indentation in this post for you, but only you can fix the indentation in your code on your computer! – BlackVegetable Mar 06 '13 at 03:37
  • Sooo glad I discovered this website. I'm a first year Software Engineering major and I feel like this won't be the first time I post here! Such a cool, helpful place! – Lizi Wiki Mar 06 '13 at 03:48
  • I hope you enjoy it here! Be sure to select one of the questions as the "accepted answer" to this question by selecting the checkmark next to the score on the answer. – BlackVegetable Mar 06 '13 at 03:53
0

You could always use an iteration counter as @BlackVegetable said:

# Finds the total of a sequence of numbers entered by user 
def find_sum(): 
     total, iterationCount = 0, 0 # multiple assignment
     entry = raw_input("Enter a value, or q to quit: ") 
     while entry != "q": 
         iterationCount += 1
         total += int(entry) 
         entry = raw_input("Enter a value, or q to quit: ") 
     print "The total is", total 
     print "Total numbers:", iterationCount

Or, you could add each number to a list and then print the sum AND the length:

# Finds the total of a sequence of numbers entered by user
def find_sum(): 
     total = []
     entry = raw_input("Enter a value, or q to quit: ") 
     while entry != "q": 
         iterationCount += 1
         total.append(int(entry))
         entry = raw_input("Enter a value, or q to quit: ") 
     print "The total is", sum(total)
     print "Total numbers:", len(total)
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94