Thread: Counter

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    Counter


    I have been scouring the internet for about and hour trying to

    find something about a input counter for python3. If thats a little

    vague I am making a averaging calculator for my math class

    and I need something that will count the amount of input so I

    so I can later divide by that number. (its not finished yet)

    #counter = 0

    total = 0

    number = 0

    while number >= 0:
    number = float(input (" \n \n \t enter here: "))

    total += number

    average = total/counter

    print (average)

    so yeah
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    You just need to increment your counter variable the same way you are updating your total variable. At least if I am understanding you correctly.

    Basically add:
    counter += 1
    Right after your:
    total += number

    Wheeee....
    python Code:
     
    def avg_of_inputs():
        counter = 0
        total = 0
        while 1:
            number = input("\nInput a number (press enter when done): ")
            try:
                total += float(number)
                counter += 1
                print("Total so far: "+str(total))
            except ValueError:
                if number == "":
                    print("\n")
                    break
                else:
                    print("That wasn't a number you dolt!\n")
        return total/counter
     
    if __name__ == "__main__":
        again = "y"         
        while again == "y":
            print ("Your avg is: "+str(avg_of_inputs()))
            while 1:
                again = input("\nAgain? (y/n): ")
                if again in ("y","n"):
                    break
                else:
                    print("\nThat wasn't an option jackass.\n")
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    If Mekire's program works, and I didn't test it for cases that I thought might work, it requires python version 3.


    Here I show a run in python2. Given bad input the program terminates with an exception.
    And in python3, with no valid input the program again terminates with an exception.

    Yes, otherwise I agree that you'd need to count the data either as Mekire did, or by appending the data to a list. Later you can use len(data), and you'll still have the data for other important statistical tests.

    Code:
    $ python /tmp/p.py 
    
    Input a number (press enter when done): x
    Traceback (most recent call last):
      File "/tmp/p.py", line 21, in <module>
        print ("Your avg is: "+str(avg_of_inputs()))
      File "/tmp/p.py", line 5, in avg_of_inputs
        number = input("\nInput a number (press enter when done): ")
      File "<string>", line 1, in <module>
    NameError: name 'x' is not defined
    
    
    
    
    
    $ python3 /tmp/p.py 
    
    Input a number (press enter when done): x
    That wasn't a number you dolt!
    
    
    Input a number (press enter when done): 
    
    
    Traceback (most recent call last):
      File "/tmp/p.py", line 21, in <module>
        print ("Your avg is: "+str(avg_of_inputs()))
      File "/tmp/p.py", line 16, in avg_of_inputs
        return total/counter
    ZeroDivisionError: division by zero
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Originally Posted by b49P23TIvg
    If Mekire's program works, and I didn't test it for cases that I thought might work, it requires python version 3.
    He specified 3 within his post so I stuck with that. Changing the inputs to raw_inputs will solve the first incompatibility. Generally I write in 2.7 with aim to run under both. Admittedly I did over look the 'divide by zero' thing though but that is also bullet proofed against easily enough.

    Cheers,
    -Mek

    Comments on this post

    • b49P23TIvg agrees : So he did. I now see the python3.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    thanks


    AHHH yes its working thanks everybody


IMN logo majestic logo threadwatch logo seochat tools logo