How to always get a positive output?

integer, python, python-3.x

Solution

You can take the absoulte value of a difference to get the "distance" between two values:

>>> abs(2 - 5)
3
>>> abs(5 - 2)
3

Problem

This is my code: ``` import random minimum=int(input("Enter minimum value: ")) maximum=int(input("Enter maximum value: ")) if minimum>maximum: temp=minimum minimum=maximum maximum=temp howMany = int(input("How many numbers do you want to generate?")) sum=0 n=1 while n<=howMany: num=random.randrange(minimum,maximum) sum+=num n+=1 print("Your random generated number is",num) print("Python's random average between", minimum, "and", maximum, "is", sum/howMany) avg=minimum+maximum/2 avgTwo=sum/howMany difference=avgTwo-avg print("The actual average of minimum and maximum is", avg) print("The difference from the calculated average and from the actual average is", difference) ``` When I am calculating the difference, I need to always get a positive number. I tried flipping the variables being substracted but I received a negative number at random times.

Original source