jeudi 18 février 2016

My program keeps getting an error "division by zero"

This program is designed to count an unspecified amount of integers, determines how many negative and positive values have been read, and computes the average and total of the input values. If the user inputs a 0, the program will end. Every time I enter a 0, I get an error stating "division by zero". The debugger says it is from the very last line. I guess it has to to with the "sum / count" part. When I use negative numbers, it says the same thing, from the same line of code. Lastly, I am unsure how to display the text "You did't enter any number". I tried doing the if else statement, but I dont think I'm doing that correctly. If anyone can help, that would be great! Thanks!


data = eval(input("Enter an integer, the input ends if it is 0: "))

count = 0

sum = 0

negCount = 0

if data != 0:

while data > 0:     

    count = count + 1

    sum = sum + data 

    data = eval(input("Enter an integer, the input ends if it is 0: "))       



    while data < 0:

        negCount = count + 1

        sum = sum + data

        data = eval(input("Enter an integer, the input ends if it is 0: "))



        while data != 0:

            sum = sum + data               

            data = eval(input("Enter an integer, the input ends if it is 0: "))

            break


        else:

            print("You didn't enter any number")                       

print("The number of positives is", count)

print("The number of negatives is", negCount)

print("The total is", sum)

print("The average is", sum / count)

Aucun commentaire:

Enregistrer un commentaire