When I try to run this code, I get incorrect maximums and minimums. Could anyone tell me how I can fix it? I am not allowed to use ‘max’ and ‘min’.
UPDATE: I have updated the code and it still doesn’t work properly.
UPDATE 2: The code works now! Thank you so much guys!
minimum=float('inf')
maximum=None
count=0
total=0
number=input ("Please enter the amount of numbers you wish to categorize: ")
while True:
num = input("Enter a number: ")
count+=1
total+=num
if num is None or num < minimum:
minimum = num
if num is None or num > maximum:
maximum = num
if count == number:
break
print "The average of your numbers is ", round ((total / count),2),"."
print 'The largest number is:', maximum,"."
print 'The smallest number is:', minimum,"."
Your initial values and conditions for
minimumandmaximumare incorrect.You could also fix this by checking if
countequals 1 instead of identity toNone.