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 
2
  • 1
    Correct your python indenting! Commented Mar 6, 2013 at 3:30
  • 1
    (Good first question, by the way -- welcome to Stack Overflow) Commented Mar 6, 2013 at 3:34

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, that was so simple. I was definitely overthinking it. Thank you for all your help and I will definite fix the indentation!
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!
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!
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.
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)

2 Comments

He was forbidden from using the len() function, according to the assignment directions though.
@BlackVegetable Ah, should have read the OP more thoroughly. I will leave this up for reference, however.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.