0

I have written this code in python which is working but when I tried to write that same program by using functions it's impossible. Help me! This is the code which is working :

count=0
total=0   
while True:
    try:
        itervar=raw_input('Enter a number: ')
        if  itervar == 'done':
            break
        itervar=float(itervar)
        count=count+1
        total=total+itervar
        average=total/count
    except:
        print 'Invalid input'
print total, ' ' , count, ' ' , average

This is not working :

def count(itervar):
    count = count+1
    return count

def total(itervar):
    total = total+itervar
    return total

def average(count,total):
    z=total/count
    return z
count=0
total=0
while True:
    try:
        itervar=raw_input('Enter a number: ')
        if  itervar == 'done':
            break
        itervar=float(itervar)
        count=count(itervar)
        total=total(itervar)

    except:
        print 'Invalid input'
print total, ' ' , count, ' ' , average(count,total)
6
  • I think you'll want to pass variable to count() and total() and set them up to receive the same. Commented Mar 21, 2017 at 15:24
  • Also, count1 and total1 aren't defined before they are used. Commented Mar 21, 2017 at 15:28
  • I tried it when I am passing itervar in count() and total(), even numeric values are 'Invalid input' it means code is jumping to except: Commented Mar 21, 2017 at 15:32
  • That's my mistake count1 and total1 are count and total. :) Commented Mar 21, 2017 at 15:33
  • Can you revise the code in your question with what you have now? Commented Mar 21, 2017 at 15:35

1 Answer 1

1

I don't think it's a good idea to name your variables the same as your function name. I cleaned that up here:

def count(this_count):
    return this_count + 1

def total(this_itervar, this_total):
    return this_itervar + this_total

def avg(this_count, this_total):
    if this_count == 0:
        return 0
    else:
        return this_total /this_count

this_count=0
this_total=0
while True:
    try:
        itervar=raw_input('Enter a number: ')
        if  itervar == 'done':
            break
        itervar=float(itervar)
        this_count = count(this_count)
        this_total = total(itervar, this_total)

    except:
        print 'Invalid input'
print str(this_total) + ' ' + str(this_count) + ' ' + str(avg(this_count, this_total))
Sign up to request clarification or add additional context in comments.

9 Comments

Thank You very much. average() is having an error (Dividing by zero error) It's the last problem I guess. :)
Try this new function. I believe average may be a reserved word, so I changed it to avg.
I had some errors in the last bit of code. Please try my edits.
Thank You very much everything works just fine now. :)
Good to hear. All the best in your future coding.
|

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.