I am supposed to have a while loop that asks for a number until the number is a negative. Then I am supposed to get a total of all of the numbers EXCEPT the negative number. As of right now my program adds up the absolute value of all the numbers input. any suggedtions on how to add all BUT the negative?

number=float(input("Enter a number: "))
total=number
while number>=0 and number<=100:
    number=float(input("Enter a number: "))
    total+=abs(number)

You can put an "if" statement right above the addition that checks the value again.
...or you can redesign how you evaluate the terminating number.

You must also validate input:

total = 0.0
while True:
    reply = input("Enter a number: ")
    try:
        number = float(reply)
    except ValueError:
        print("Invalid value")
    else:
        if 0 <= number <= 100:
            total += number
        else:
            break
print("The sum is {0:.2f}.".format(total))

Your original code would look something like this, but take note of the try/except as that is usual way of getting input.

number=0
while number>=0 and number<=100:
    ## will only add to total if the input from the previous loop is 0 <= number <= 100
    total+=abs(number)     ## adds number==zero on the first pass
    number=float(input("Enter a number: "))
number=0
while number>=0 and number<=100:
    ## will only add to total if the input from the previous loop is 0 <= number <= 100
    total+=abs(number)     ## adds number==zero on the first pass
    number=float(input("Enter a number: "))

The abs does not belong in loop, as the number is checked in range by while condition.

number=0
while number>=0 and number<=100:
    ## will only add to total if the input from the previous loop is 0 <= number <= 100
    total+=number     ## adds number==zero on the first pass
    number=float(input("Enter a number: "))

And this would crash when user inputs 'lOO', which Gribouillis' try..except would catch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.